了解遗留的帕斯卡
Understanding legacy pascal
所以我需要了解这段代码的作用。我不知道帕斯卡或密码学,并且正在努力了解这里发生的事情。我需要将 SHA1DigestToHex
逆向工程到 scala 中并且完全迷失在学习 pascal 之外。你能告诉我这个功能是做什么的吗?或者我该如何解决?
Function SHA1DigestToHex (const Digest : T160BitDigest) : String;
Begin
Result := DigestToHex (Digest, Sizeof (Digest));
End;
Function DigestToHex (const Digest; const Size : Integer) : String;
Begin
SetLength (Result, Size * 2);
DigestToHexBuf (Digest, Size, Pointer (Result)^);
End;
Procedure DigestToHexBuf (const Digest; const Size : Integer; const Buf);
const s_HexDigitsLower : String [16] = '0123456789abcdef';
var I : Integer;
P : PChar;
Q : PByte;
Begin
P := @Buf;;
Assert (Assigned (P), 'Assigned (Buf)');
Q := @Digest;
Assert (Assigned (Q), 'Assigned (Digest)');
For I := 0 to Size - 1 do
begin
P^ := s_HexDigitsLower [Q^ shr 4 + 1];
Inc (P);
P^ := s_HexDigitsLower [Q^ and 15 + 1];
Inc (P);
Inc (Q);
end;
End;
更新
type
PByte = ^Byte;
PWord = ^Word;
PLongWord = ^LongWord;
T128BitDigest = record
case integer of
0 : (Int64s : Array [0..1] of Int64);
1 : (Longs : Array [0..3] of LongWord);
2 : (Words : Array [0..7] of Word);
3 : (Bytes : Array [0..15] of Byte);
end;
P128BitDigest = ^T128BitDigest;
T160BitDigest = record
case integer of
0 : (Longs : Array [0..4] of LongWord);
1 : (Words : Array [0..9] of Word);
2 : (Bytes : Array [0..19] of Byte);
end;
P160BitDigest = ^T160BitDigest;
const
MaxHashDigestSize = Sizeof (T160BitDigest);
Procedure DigestToHexBuf (const Digest; const Size : Integer; const Buf);
Function DigestToHex (const Digest; const Size : Integer) : String;
Function Digest128Equal (const Digest1, Digest2 : T128BitDigest) : Boolean;
Function Digest160Equal (const Digest1, Digest2 : T160BitDigest) : Boolean;
它只是将作为Buf
传入的二进制缓冲区的字节转换为表示Digest
中相同字节的十六进制数字字符串。
例如如果 Buf 是字节数组(0x12、0x34、0x56),那么之后 Digest 将是 '123456'。
这是一个更简单的 Pascal (Delphi) 版本,它做同样的事情:
function SHA1DigestToHex2(const Digest : T160BitDigest) : string;
const s_HexDigitsLower : array[0..15] of char = '0123456789abcdef';
var
i, j: Integer;
Begin
SetLength(Result, sizeof(Digest) * 2);
i := 1;
j := 0;
while j < sizeof(Digest) do begin
Result[i] := s_HexDigitsLower[Digest.Bytes[j] shr 4];
Result[i+1] := s_HexDigitsLower[Digest.Bytes[j] and $F];
inc(i, 2);
inc(j);
end;
End;
所以我需要了解这段代码的作用。我不知道帕斯卡或密码学,并且正在努力了解这里发生的事情。我需要将 SHA1DigestToHex
逆向工程到 scala 中并且完全迷失在学习 pascal 之外。你能告诉我这个功能是做什么的吗?或者我该如何解决?
Function SHA1DigestToHex (const Digest : T160BitDigest) : String;
Begin
Result := DigestToHex (Digest, Sizeof (Digest));
End;
Function DigestToHex (const Digest; const Size : Integer) : String;
Begin
SetLength (Result, Size * 2);
DigestToHexBuf (Digest, Size, Pointer (Result)^);
End;
Procedure DigestToHexBuf (const Digest; const Size : Integer; const Buf);
const s_HexDigitsLower : String [16] = '0123456789abcdef';
var I : Integer;
P : PChar;
Q : PByte;
Begin
P := @Buf;;
Assert (Assigned (P), 'Assigned (Buf)');
Q := @Digest;
Assert (Assigned (Q), 'Assigned (Digest)');
For I := 0 to Size - 1 do
begin
P^ := s_HexDigitsLower [Q^ shr 4 + 1];
Inc (P);
P^ := s_HexDigitsLower [Q^ and 15 + 1];
Inc (P);
Inc (Q);
end;
End;
更新
type
PByte = ^Byte;
PWord = ^Word;
PLongWord = ^LongWord;
T128BitDigest = record
case integer of
0 : (Int64s : Array [0..1] of Int64);
1 : (Longs : Array [0..3] of LongWord);
2 : (Words : Array [0..7] of Word);
3 : (Bytes : Array [0..15] of Byte);
end;
P128BitDigest = ^T128BitDigest;
T160BitDigest = record
case integer of
0 : (Longs : Array [0..4] of LongWord);
1 : (Words : Array [0..9] of Word);
2 : (Bytes : Array [0..19] of Byte);
end;
P160BitDigest = ^T160BitDigest;
const
MaxHashDigestSize = Sizeof (T160BitDigest);
Procedure DigestToHexBuf (const Digest; const Size : Integer; const Buf);
Function DigestToHex (const Digest; const Size : Integer) : String;
Function Digest128Equal (const Digest1, Digest2 : T128BitDigest) : Boolean;
Function Digest160Equal (const Digest1, Digest2 : T160BitDigest) : Boolean;
它只是将作为Buf
传入的二进制缓冲区的字节转换为表示Digest
中相同字节的十六进制数字字符串。
例如如果 Buf 是字节数组(0x12、0x34、0x56),那么之后 Digest 将是 '123456'。
这是一个更简单的 Pascal (Delphi) 版本,它做同样的事情:
function SHA1DigestToHex2(const Digest : T160BitDigest) : string;
const s_HexDigitsLower : array[0..15] of char = '0123456789abcdef';
var
i, j: Integer;
Begin
SetLength(Result, sizeof(Digest) * 2);
i := 1;
j := 0;
while j < sizeof(Digest) do begin
Result[i] := s_HexDigitsLower[Digest.Bytes[j] shr 4];
Result[i+1] := s_HexDigitsLower[Digest.Bytes[j] and $F];
inc(i, 2);
inc(j);
end;
End;