📄 lbclass.pas
字号:
var
BlkCount, BlkSize : Cardinal;
begin
BlkSize := SizeOf(TRDLBlock);
BlkCount := (InBufSize div BlkSize) + 1;
Result := BlkCount * BlkSize;
end;
{ == TLbHash =============================================================== }
constructor TLbHash.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
end;
{ -------------------------------------------------------------------------- }
destructor TLbHash.Destroy;
begin
inherited Destroy;
end;
{ == TLbMD5 ================================================================ }
constructor TLbMD5.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
end;
{ -------------------------------------------------------------------------- }
destructor TLbMD5.Destroy;
begin
inherited Destroy;
end;
{ -------------------------------------------------------------------------- }
procedure TLbMD5.GetDigest(var Digest : TMD5Digest);
begin
Move(FDigest, Digest, SizeOf(Digest));
end;
{ -------------------------------------------------------------------------- }
procedure TLbMD5.HashBuffer(const Buf; BufSize : Cardinal);
begin
HashMD5(FDigest, Buf, BufSize);
end;
{ -------------------------------------------------------------------------- }
procedure TLbMD5.HashFile(const AFileName : string);
var
FS : TFileStream;
begin
FS := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
try
HashStream(FS);
finally
FS.Free;
end;
end;
{ -------------------------------------------------------------------------- }
procedure TLbMD5.HashStream(AStream: TStream);
var
Context : TMD5Context;
BufSize : Integer;
begin
InitMD5(Context);
BufSize := AStream.Read(FBuf, SizeOf(FBuf));
while (BufSize > 0) do begin
UpdateMD5(Context, FBuf, BufSize);
BufSize := AStream.Read(FBuf, SizeOf(FBuf));
end;
FinalizeMD5(Context, FDigest);
end;
{ -------------------------------------------------------------------------- }
procedure TLbMD5.HashString(const AStr : string);
begin
StringHashMD5(FDigest, AStr);
end;
{ == TLbSHA1 =============================================================== }
constructor TLbSHA1.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
end;
{ -------------------------------------------------------------------------- }
destructor TLbSHA1.Destroy;
begin
inherited Destroy;
end;
{ -------------------------------------------------------------------------- }
procedure TLbSHA1.GetDigest(var Digest : TSHA1Digest);
begin
Move(FDigest, Digest, SizeOf(Digest));
end;
{ -------------------------------------------------------------------------- }
procedure TLbSHA1.HashBuffer(const Buf; BufSize : Cardinal);
begin
HashSHA1(FDigest, Buf, BufSize);
end;
{ -------------------------------------------------------------------------- }
procedure TLbSHA1.HashFile(const AFileName : string);
var
FS : TFileStream;
begin
FS := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
try
HashStream(FS);
finally
FS.Free;
end;
end;
{ -------------------------------------------------------------------------- }
procedure TLbSHA1.HashStream(AStream: TStream);
var
Context : TSHA1Context;
BufSize : Integer;
begin
InitSHA1(Context);
BufSize := AStream.Read(FBuf, SizeOf(FBuf));
while (BufSize > 0) do begin
UpdateSHA1(Context, FBuf, BufSize);
BufSize := AStream.Read(FBuf, SizeOf(FBuf));
end;
FinalizeSHA1(Context, FDigest);
end;
{ -------------------------------------------------------------------------- }
procedure TLbSHA1.HashString(const AStr : string);
begin
StringHashSHA1(FDigest, AStr);
end;
{ == TLbSCStream =========================================================== }
constructor TLbSCStream.Create(const Key; KeySize : Integer);
{-create the stream and initialize context}
begin
inherited Create;
Reinitialize(Key, KeySize);
end;
{ -------------------------------------------------------------------------- }
procedure TLbSCStream.Reinitialize(const Key; KeySize : Integer);
{-reinitialize context and reposition to beginning of stream}
begin
ChangeKey(Key, KeySize);
Position := 0;
end;
{ -------------------------------------------------------------------------- }
procedure TLbSCStream.ChangeKey(const Key; KeySize : Integer);
{-reinitialize using a new key}
begin
InitEncryptLSC(Key, KeySize, FContext);
end;
{ -------------------------------------------------------------------------- }
function TLbSCStream.Read(var Buffer; Count : LongInt) : LongInt;
{-read Count bytes into Buffer, return bytes read}
begin
Result := inherited Read(Buffer, Count);
EncryptLSC(FContext, Buffer, Count);
end;
{ -------------------------------------------------------------------------- }
function TLbSCStream.Write(const Buffer; Count : LongInt) : LongInt;
{-write Count bytes to Buffer, return bytes written}
var
Buf : Pointer;
begin
GetMem(Buf, Count);
try
Move(Buffer, Buf^, Count);
EncryptLSC(FContext, Buf^, Count);
Result := inherited Write(Buf^, Count);
finally
FreeMem(Buf, Count);
end;
end;
{ == TLbSCFileStream ======================================================= }
constructor TLbSCFileStream.Create(const FileName : string; Mode : Word;
const Key; KeySize : Integer);
{-create the stream and initialize context}
begin
inherited Create(FileName, Mode);
Reinitialize(Key, KeySize);
end;
{ -------------------------------------------------------------------------- }
procedure TLbSCFileStream.Reinitialize(const Key; KeySize : Integer);
{-reinitialize context and reposition to beginning of stream}
begin
ChangeKey(Key, KeySize);
Position := 0;
end;
{ -------------------------------------------------------------------------- }
procedure TLbSCFileStream.ChangeKey(const Key; KeySize : Integer);
{-reinitialize using a new key}
begin
InitEncryptLSC(Key, KeySize, FContext);
end;
{ -------------------------------------------------------------------------- }
function TLbSCFileStream.Read(var Buffer; Count : LongInt) : LongInt;
{-read Count bytes into Buffer, return bytes read}
begin
Result := inherited Read(Buffer, Count);
EncryptLSC(FContext, Buffer, Count);
end;
{ -------------------------------------------------------------------------- }
function TLbSCFileStream.Write(const Buffer; Count : LongInt) : LongInt;
{-write Count bytes to Buffer, return bytes written}
var
Buf : Pointer;
begin
GetMem(Buf, Count);
try
Move(Buffer, Buf^, Count);
EncryptLSC(FContext, Buf^, Count);
Result := inherited Write(Buf^, Count);
finally
FreeMem(Buf, Count);
end;
end;
{ == TLbRNG32Stream ======================================================== }
constructor TLbRNG32Stream.Create(const Key : LongInt);
{-create the stream and initialize context}
begin
inherited Create;
Reinitialize(Key);
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG32Stream.Reinitialize(const Key : LongInt);
{-reinitialize context and reposition to beginning of stream}
begin
ChangeKey(Key);
Position := 0;
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG32Stream.ChangeKey(const Key : LongInt);
{-reinitialize using a new key}
begin
InitEncryptRNG32(Key, FContext);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG32Stream.Read(var Buffer; Count : LongInt) : LongInt;
{-read Count bytes into Buffer, return bytes read}
begin
Result := inherited Read(Buffer, Count);
EncryptRNG32(FContext, Buffer, Count);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG32Stream.Write(const Buffer; Count : LongInt) : LongInt;
{-write Count bytes to Buffer, return bytes written}
var
Buf : Pointer;
begin
GetMem(Buf, Count);
try
Move(Buffer, Buf^, Count);
EncryptRNG32(FContext, Buf^, Count);
Result := inherited Write(Buf^, Count);
finally
FreeMem(Buf, Count);
end;
end;
{ == TLbRNG32FileStream ==================================================== }
constructor TLbRNG32FileStream.Create(const FileName : string; Mode : Word;
const Key : LongInt);
{-create the stream and initialize context}
begin
inherited Create(FileName, Mode);
Reinitialize(Key);
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG32FileStream.Reinitialize(const Key : LongInt);
{-reinitialize context and reposition to beginning of stream}
begin
ChangeKey(Key);
Position := 0;
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG32FileStream.ChangeKey(const Key : LongInt);
{-reinitialize using a new key}
begin
InitEncryptRNG32(Key, FContext);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG32FileStream.Read(var Buffer; Count : LongInt) : LongInt;
{-read Count bytes into Buffer, return bytes read}
begin
Result := inherited Read(Buffer, Count);
EncryptRNG32(FContext, Buffer, Count);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG32FileStream.Write(const Buffer; Count : LongInt) : LongInt;
{-write Count bytes to Buffer, return bytes written}
var
Buf : Pointer;
begin
GetMem(Buf, Count);
try
Move(Buffer, Buf^, Count);
EncryptRNG32(FContext, Buf^, Count);
Result := inherited Write(Buf^, Count);
finally
FreeMem(Buf, Count);
end;
end;
{ == TLbRNG64Stream ======================================================== }
constructor TLbRNG64Stream.Create(const KeyHi, KeyLo : LongInt);
{-create the stream and initialize context}
begin
inherited Create;
Reinitialize(KeyHi, KeyLo);
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG64Stream.Reinitialize(const KeyHi, KeyLo : LongInt);
{-reinitialize context and reposition to beginning of stream}
begin
ChangeKey(KeyHi, KeyLo);
Position := 0;
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG64Stream.ChangeKey(const KeyHi, KeyLo : LongInt);
{-reinitialize using a new key}
begin
InitEncryptRNG64(KeyHi, KeyLo, FContext);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG64Stream.Read(var Buffer; Count : LongInt) : LongInt;
{-read Count bytes into Buffer, return bytes read}
begin
Result := inherited Read(Buffer, Count);
EncryptRNG64(FContext, Buffer, Count);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG64Stream.Write(const Buffer; Count : LongInt) : LongInt;
{-write Count bytes to Buffer, return bytes written}
var
Buf : Pointer;
begin
GetMem(Buf, Count);
try
Move(Buffer, Buf^, Count);
EncryptRNG64(FContext, Buf^, Count);
Result := inherited Write(Buf^, Count);
finally
FreeMem(Buf, Count);
end;
end;
{ == TLbRNG64FileStream ==================================================== }
constructor TLbRNG64FileStream.Create(const FileName : string; Mode : Word;
const KeyHi, KeyLo : LongInt);
{-create the stream and initialize context}
begin
inherited Create(FileName, Mode);
Reinitialize(KeyHi, KeyLo);
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG64FileStream.Reinitialize(const KeyHi, KeyLo : LongInt);
{-reinitialize context and reposition to beginning of stream}
begin
ChangeKey(KeyHi, KeyLo);
Position := 0;
end;
{ -------------------------------------------------------------------------- }
procedure TLbRNG64FileStream.ChangeKey(const KeyHi, KeyLo : LongInt);
{-reinitialize using a new key}
begin
InitEncryptRNG64(KeyHi, KeyLo, FContext);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG64FileStream.Read(var Buffer; Count : LongInt) : LongInt;
{-read Count bytes into Buffer, return bytes read}
begin
Result := inherited Read(Buffer, Count);
EncryptRNG64(FContext, Buffer, Count);
end;
{ -------------------------------------------------------------------------- }
function TLbRNG64FileStream.Write(const Buffer; Count : LongInt) : LongInt;
{-write Count bytes to Buffer, return bytes written}
var
Buf : Pointer;
begin
GetMem(Buf, Count);
try
Move(Buffer, Buf^, Count);
EncryptRNG64(FContext, Buf^, Count);
Result := inherited Write(Buf^, Count);
finally
FreeMem(Buf, Count);
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -