unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
btnVerify: TButton;
btnSignature: TButton;
SpeedButton1: TSpeedButton;
OpenDialog1: TOpenDialog;
Label1: TLabel;
procedure btnVerifyClick(Sender: TObject);
procedure btnSignatureClick(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function VerifyStream(
FS: TStream;
const aSignature: string
): Boolean;
var
L: Integer;
Buf: string;
begin
Result := False;
L := Length(aSignature);
if (L > 0) and (FS.Size > L) then
begin
FS.Seek(-L, soFromEnd);
SetLength(Buf, L);
FS.ReadBuffer(Buf[1], L);
Result := CompareStr(Buf, aSignature) = 0;
end;
end; procedure SignatureFile(const aFileName: string; const aSignature: string);
var
FS: TFileStream;
begin
Assert(aSignature <> ' ');
FS := TFileStream.Create(
aFileName,
fmOpenReadWrite or fmShareExclusive
); with FS do
try
if not VerifyStream(FS, aSignature) then
begin
Seek(0, soFromEnd);
WriteBuffer(aSignature[1], Length(aSignature));
end;
finally
Destroy();
end;
end; function VerifyFile(const aFileName, aSignature: string): Boolean;
var
FS: TFileStream;
begin
//FS := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyWrite); 每次重复写,写的次数多
FS := TFileStream.Create(aFileName, fmOpenRead); //只写一次
try
Result := VerifyStream(FS, aSignature);
finally
FS.Destroy();
end;
end; const
Signature = 'CRC TEST12 ';
procedure TForm1.btnVerifyClick(Sender: TObject);
begin
if not VerifyFile(Edit1.Text, Signature) then
raise Exception.Create( 'The file is not signatured ');
ShowMessage( 'ok ');
ShowMessage( Signature);
end;procedure TForm1.btnSignatureClick(Sender: TObject);
begin
SignatureFile(Edit1.Text, Signature);end;procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text := OpenDialog1.FileName; end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
btnVerify: TButton;
btnSignature: TButton;
SpeedButton1: TSpeedButton;
OpenDialog1: TOpenDialog;
Label1: TLabel;
procedure btnVerifyClick(Sender: TObject);
procedure btnSignatureClick(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function VerifyStream(
FS: TStream;
const aSignature: string
): Boolean;
var
L: Integer;
Buf: string;
begin
Result := False;
L := Length(aSignature);
if (L > 0) and (FS.Size > L) then
begin
FS.Seek(-L, soFromEnd);
SetLength(Buf, L);
FS.ReadBuffer(Buf[1], L);
Result := CompareStr(Buf, aSignature) = 0;
end;
end; procedure SignatureFile(const aFileName: string; const aSignature: string);
var
FS: TFileStream;
begin
Assert(aSignature <> ' ');
FS := TFileStream.Create(
aFileName,
fmOpenReadWrite or fmShareExclusive
); with FS do
try
if not VerifyStream(FS, aSignature) then
begin
Seek(0, soFromEnd);
WriteBuffer(aSignature[1], Length(aSignature));
end;
finally
Destroy();
end;
end; function VerifyFile(const aFileName, aSignature: string): Boolean;
var
FS: TFileStream;
begin
//FS := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyWrite); 每次重复写,写的次数多
FS := TFileStream.Create(aFileName, fmOpenRead); //只写一次
try
Result := VerifyStream(FS, aSignature);
finally
FS.Destroy();
end;
end; const
Signature = 'CRC TEST12 ';
procedure TForm1.btnVerifyClick(Sender: TObject);
begin
if not VerifyFile(Edit1.Text, Signature) then
raise Exception.Create( 'The file is not signatured ');
ShowMessage( 'ok ');
ShowMessage( Signature);
end;procedure TForm1.btnSignatureClick(Sender: TObject);
begin
SignatureFile(Edit1.Text, Signature);end;procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text := OpenDialog1.FileName; end;end.
// Signature = 'CRC TEST12 ';procedure TForm1.btnVerifyClick(Sender: TObject);
begin
if not VerifyFile(Edit1.Text, Edit2.Text) then
raise Exception.Create('The file is not signatured ');
ShowMessage('ok');
ShowMessage(Edit2.Text);
end;procedure TForm1.btnSignatureClick(Sender: TObject);
begin
SignatureFile(Edit1.Text, Edit2.Text);
end;