Implement HMACStream and HMACFile
@MHumm 已經在處理了。
開始於 2024年4月30日。
評估
這個 Issue 還沒有評估資料。
描述
I want to suggest a combination of the following methods:
- TDECHashAuthentication.HMAC combined with TDECHashExtended.CalcStream
- TDECHashAuthentication.HMAC combined with TDECHashExtended.CalcFile
Background information: I want to encrypt a large file which does not fit into the memory, and I would like to do "Encrypt-then-HMAC". So I need to HMAC the whole file or stream. (Pre-hasing is not a solution, because it is insecure)
Here is the code I wrote (actually copied and modified) and tested.
type
TDECHashExtendedAuthentication = class helper for TDECHashAuthentication
class function HMACFile(const Key: TBytes; const FileName: string;
const OnProgress:TDECProgressEvent = nil): TBytes;
class function HMACStream(const Key: TBytes; const Stream: TStream; Size: Int64;
const OnProgress:TDECProgressEvent = nil): TBytes;
end;
{ TDECHashExtendedAuthentication }
class function TDECHashExtendedAuthentication.HMACFile(const Key: TBytes;
const FileName: string; const OnProgress: TDECProgressEvent): TBytes;
var
fs: TFileStream;
begin
fs := TFileStream.Create(FileName, fmOpenRead);
try
HMACStream(Key, fs, fs.Size, OnProgress);
finally
FreeAndNil(fs);
end;
end;
class function TDECHashExtendedAuthentication.HMACStream(const Key: TBytes;
const Stream: TStream; Size: Int64;
const OnProgress: TDECProgressEvent = nil): TBytes;
const
CONST_UINT_OF_0x36 = $3636363636363636;
CONST_UINT_OF_0x5C = $5C5C5C5C5C5C5C5C;
var
HashInstance: TDECHashAuthentication;
InnerKeyPad, OuterKeyPad: array of Byte;
I, KeyLength, BlockSize, DigestLength: Integer;
begin
// Taken from TDECHashAuthentication.HMAC and changed HashInstance.Calc to HashInstance.CalcStream for the message
HashInstance := TDECHashAuthenticationClass(self).Create;
try
BlockSize := HashInstance.BlockSize; // 64 for sha1, ...
DigestLength := HashInstance.DigestSize;
KeyLength := Length(Key);
SetLength(InnerKeyPad, BlockSize);
SetLength(OuterKeyPad, BlockSize);
I := 0;
if KeyLength > BlockSize then
begin
Result := HashInstance.CalcBytes(Key);
KeyLength := DigestLength;
end
else
Result := Key;
while I <= KeyLength - SizeOf(NativeUInt) do
begin
PNativeUInt(@InnerKeyPad[I])^ := PNativeUInt(@Result[I])^ xor NativeUInt(CONST_UINT_OF_0x36);
PNativeUInt(@OuterKeyPad[I])^ := PNativeUInt(@Result[I])^ xor NativeUInt(CONST_UINT_OF_0x5C);
Inc(I, SizeOf(NativeUInt));
end;
while I < KeyLength do
begin
InnerKeyPad[I] := Result[I] xor $36;
OuterKeyPad[I] := Result[I] xor $5C;
Inc(I);
end;
while I <= BlockSize - SizeOf(NativeUInt) do
begin
PNativeUInt(@InnerKeyPad[I])^ := NativeUInt(CONST_UINT_OF_0x36);
PNativeUInt(@OuterKeyPad[I])^ := NativeUInt(CONST_UINT_OF_0x5C);
Inc(I, SizeOf(NativeUInt));
end;
while I < BlockSize do
begin
InnerKeyPad[I] := $36;
OuterKeyPad[I] := $5C;
Inc(I);
end;
HashInstance.Init;
HashInstance.Calc(InnerKeyPad[0], BlockSize);
if Size > 0 then
TDECHashExtended(HashInstance).CalcStream(Stream, Size, OnProgress, false);
HashInstance.Done;
Result := HashInstance.DigestAsBytes;
HashInstance.Init;
HashInstance.Calc(OuterKeyPad[0], BlockSize);
HashInstance.Calc(Result[0], DigestLength);
HashInstance.Done;
Result := HashInstance.DigestAsBytes;
finally
HashInstance.Free;
end;
end;
- 主要語言
- HTML
- 星號
- 286
- 分支
- 78
- 平均合併
- 1 天 23 分鐘
- 30 天內合併 PR
- 5
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
MHumm/DelphiEncryptionCompendium 的其他 Issue
-
CRC64 - PKI Addition可能重新可做 @MHumm 於 152 天前認領,目前沒有進行中的 PR。 未關閉enhancement help wanted
MHumm/DelphiEncryptionCompendium#93 · 1 則留言 · 已指派 1 人 ·
-
Missing PUInt8Array type in the GetIt version.可能重新可做 @MHumm 於 164 天前認領,目前沒有進行中的 PR。 未關閉bug delphi
MHumm/DelphiEncryptionCompendium#92 · 2 則留言 · 已指派 1 人 ·
-
Confused by NEXTGEN directive可能重新可做 @MHumm 於 535 天前認領,目前沒有進行中的 PR。 未關閉bug
MHumm/DelphiEncryptionCompendium#88 · 8 則留言 · 已指派 1 人 ·
-
Authenticated tag is wrong on streams可能重新可做 @MHumm 於 545 天前認領,目前沒有進行中的 PR。 未關閉bug
MHumm/DelphiEncryptionCompendium#87 · 20 則留言 · 已指派 1 人 ·
-
Support for AEGIS 256可能重新可做 @MHumm 於 855 天前認領,目前沒有進行中的 PR。 未關閉enhancement help wanted
MHumm/DelphiEncryptionCompendium#77 · 3 則留言 · 1 個 reaction · 已指派 1 人 ·