📄 msghash.pas
字号:
else Result := GetShortClassName(HashClass);
end;
destructor THash.Destroy;
begin
FillChar(DigestKey^, DigestKeySize, 0);
inherited Destroy;
end;
procedure THash.Init;
begin
Protect(True);
end;
procedure THash.Calc(const Data; DataSize: Integer);
begin
end;
procedure THash.Protect(IsInit: Boolean);
begin
{
if Protection <> nil then
if IsObject(Protection, TMAC) then
begin
with TMAC(Protection) do
if IsInit then Init(Self) else Done(Self);
end else
if not IsInit then
Protection.CodeBuffer(DigestKey^, DigestKeySize, paScramble);
}
end;
procedure THash.Done;
begin
Protect(False);
end;
function THash.DigestKey: Pointer;
begin
Result := GetTestVector;
end;
class function THash.DigestKeySize: Integer;
begin
Result := 0;
end;
function THash.DigestStr(Format: Integer): String;
begin
Result := StrToFormat(DigestKey, DigestKeySize, Format);
end;
class function THash.TestVector: Pointer;
begin
Result := GetTestVector;
end;
class function THash.CalcStream(const Stream: TStream; StreamSize: Integer; Protection: TProtection; Format: Integer): String;
const
maxBufSize = 1024 * 4; {Buffersize for File, Stream-Access}
var
Buf: Pointer;
BufSize: Integer;
Size: Integer;
Hash: THash;
begin
Hash := Create(Protection);
with Hash do
try
Buf := AllocMem(maxBufSize);
Init;
if StreamSize < 0 then
{if Size < 0 then reset the Position, otherwise, calc with the specific
Size and from the aktual Position in the Stream}
begin
Stream.Position := 0;
StreamSize := Stream.Size;
end;
Size := StreamSize;
DoProgress(Hash, 0, Size);
repeat
BufSize := StreamSize;
if BufSize > maxBufSize then BufSize := maxBufSize;
BufSize := Stream.Read(Buf^, BufSize);
if BufSize <= 0 then Break;
Calc(Buf^, BufSize);
Dec(StreamSize, BufSize);
DoProgress(Hash, Size - StreamSize, Size);
until BufSize <= 0;
Done;
Result := StrToFormat(DigestKey, DigestKeySize, Format);
finally
DoProgress(Hash, 0, 0);
Free;
ReallocMem(Buf, 0);
end;
end;
class function THash.CalcString(const Data: String; Protection: TProtection; Format: Integer): String;
begin
with Create(Protection) do
try
Init;
Calc(PChar(Data)^, Length(Data));
Done;
Result := StrToFormat(DigestKey, DigestKeySize, Format);
finally
Free;
end;
end;
class function THash.CalcFile(const FileName: String; Protection: TProtection; Format: Integer): String;
var
S: TFileStream;
begin
S := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
Result := CalcStream(S, S.Size, Protection, Format);
finally
S.Free;
end;
end;
class function THash.CalcBuffer(const Buffer; BufferSize: Integer; Protection: TProtection; Format: Integer): String;
begin
with Create(Protection) do {create an Object from my Classtype}
try
Init;
Calc(Buffer, BufferSize);
Done;
Result := StrToFormat(DigestKey, DigestKeySize, Format);
finally
Free; {destroy it}
end;
end;
class function THash.SelfTest: Boolean;
var
Test: String;
begin
Test := CalcBuffer(GetTestVector^, 32, nil, fmtCOPY);
Result := InitTestIsOk and (MemCompare(PChar(Test), TestVector, Length(Test)) = 0);
end;
procedure THash.CodeInit(Action: TPAction);
begin
Init;
if Action = paWipe then RndXORBuffer(RndTimeSeed, DigestKey^, DigestKeySize);
inherited CodeInit(Action);
end;
procedure THash.CodeDone(Action: TPAction);
begin
inherited CodeDone(Action);
if (Action <> paCalc) then Init else Done;
end;
procedure THash.CodeBuf(var Buffer; const BufferSize: Integer; Action: TPAction);
var
BPtr: PByte;
CSize,DSize,BSize: Integer;
begin
if Action <> paDecode then inherited CodeBuf(Buffer, BufferSize, Action);
if Action in Actions then
begin
BPtr := @Buffer;
if BPtr = nil then Exit;
DSize := DigestKeySize;
CSize := BufferSize;
if Action = paCalc then
begin
Calc(Buffer, BufferSize);
end else
begin
if Action in [paScramble, paWipe] then
begin
while CSize > 0 do
begin
BSize := CSize;
if BSize > DSize then BSize := DSize;
Calc(BPtr^, BSize);
Done;
Move(DigestKey^, BPtr^, BSize);
Dec(CSize, BSize);
Inc(BPtr, BSize);
end;
end else
while CSize > 0 do
begin
BSize := DSize;
if BSize > CSize then BSize := CSize;
Calc(DigestKey^, DSize);
Done;
XORBuffers(DigestKey, BPtr, BSize, BPtr);
Dec(CSize, BSize);
Inc(BPtr, BSize);
end;
end;
end;
if Action = paDecode then
inherited CodeBuf(Buffer, BufferSize, Action);
end;
class function THash_MD4.TestVector: Pointer; assembler;
asm
MOV EAX,OFFSET @Vector
RET
@Vector: DB 025h,0EAh,0BFh,0CCh,08Ch,0C9h,06Fh,0D9h
DB 02Dh,0CFh,07Eh,0BDh,07Fh,087h,07Ch,07Ch
end;
procedure THash_MD4.Transform(Buffer: PIntArray);
{calculate the Digest, fast}
var
A, B, C, D: LongWord;
begin
A := FDigest[0];
B := FDigest[1];
C := FDigest[2];
D := FDigest[3];
Inc(A, Buffer[ 0] + (B and C or not B and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 1] + (A and B or not A and C)); D := D shl 7 or D shr 25;
Inc(C, Buffer[ 2] + (D and A or not D and B)); C := C shl 11 or C shr 21;
Inc(B, Buffer[ 3] + (C and D or not C and A)); B := B shl 19 or B shr 13;
Inc(A, Buffer[ 4] + (B and C or not B and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 5] + (A and B or not A and C)); D := D shl 7 or D shr 25;
Inc(C, Buffer[ 6] + (D and A or not D and B)); C := C shl 11 or C shr 21;
Inc(B, Buffer[ 7] + (C and D or not C and A)); B := B shl 19 or B shr 13;
Inc(A, Buffer[ 8] + (B and C or not B and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 9] + (A and B or not A and C)); D := D shl 7 or D shr 25;
Inc(C, Buffer[10] + (D and A or not D and B)); C := C shl 11 or C shr 21;
Inc(B, Buffer[11] + (C and D or not C and A)); B := B shl 19 or B shr 13;
Inc(A, Buffer[12] + (B and C or not B and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[13] + (A and B or not A and C)); D := D shl 7 or D shr 25;
Inc(C, Buffer[14] + (D and A or not D and B)); C := C shl 11 or C shr 21;
Inc(B, Buffer[15] + (C and D or not C and A)); B := B shl 19 or B shr 13;
Inc(A, Buffer[ 0] + $5A827999 + (B and C or B and D or C and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 4] + $5A827999 + (A and B or A and C or B and C)); D := D shl 5 or D shr 27;
Inc(C, Buffer[ 8] + $5A827999 + (D and A or D and B or A and B)); C := C shl 9 or C shr 23;
Inc(B, Buffer[12] + $5A827999 + (C and D or C and A or D and A)); B := B shl 13 or B shr 19;
Inc(A, Buffer[ 1] + $5A827999 + (B and C or B and D or C and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 5] + $5A827999 + (A and B or A and C or B and C)); D := D shl 5 or D shr 27;
Inc(C, Buffer[ 9] + $5A827999 + (D and A or D and B or A and B)); C := C shl 9 or C shr 23;
Inc(B, Buffer[13] + $5A827999 + (C and D or C and A or D and A)); B := B shl 13 or B shr 19;
Inc(A, Buffer[ 2] + $5A827999 + (B and C or B and D or C and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 6] + $5A827999 + (A and B or A and C or B and C)); D := D shl 5 or D shr 27;
Inc(C, Buffer[10] + $5A827999 + (D and A or D and B or A and B)); C := C shl 9 or C shr 23;
Inc(B, Buffer[14] + $5A827999 + (C and D or C and A or D and A)); B := B shl 13 or B shr 19;
Inc(A, Buffer[ 3] + $5A827999 + (B and C or B and D or C and D)); A := A shl 3 or A shr 29;
Inc(D, Buffer[ 7] + $5A827999 + (A and B or A and C or B and C)); D := D shl 5 or D shr 27;
Inc(C, Buffer[11] + $5A827999 + (D and A or D and B or A and B)); C := C shl 9 or C shr 23;
Inc(B, Buffer[15] + $5A827999 + (C and D or C and A or D and A)); B := B shl 13 or B shr 19;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -