【发布时间】:2021-11-19 22:34:56
【问题描述】:
我想使用 DCPCrypt 将字符串转换为 sha256 哈希。 表单包含输入字符串的编辑、转换字符串的按钮和显示输出 sha256 哈希的另一个编辑。 我写了这段代码:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, DCPsha256, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
DCP_sha256_1: TDCP_sha256;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function getsha256(S: String): String;
var
Hash: TDCP_sha256;
Digest: array[0..31] of byte; // sha256 produces a 256bit digest (32bytes)
Source: string;
i: integer;
str1: string;
begin
Source:= S; // here your string for get sha256
if Source <> '' then
begin
Hash:= TDCP_sha256.Create(nil); // create the hash
Hash.Init; // initialize it
Hash.UpdateStr(Source);
Hash.Final(Digest); // produce the digest
str1:= '';
for i:= 0 to 31 do
str1:= str1 + IntToHex(Digest[i],2);
//form1.Edit2.Text:= s; // display the digest in lower case
Form1.Edit2.Text:=UpperCase(str1); // display the digest in capital letter
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
getsha256(Edit1.Text); // show the sha256 of string in edit1
end;
end.
它有效。问题是我收到了来自 lazarus 的警告和提示。
- 警告:局部变量“Digest”似乎未初始化;
- 警告:似乎没有设置函数结果。
【问题讨论】: