-
Notifications
You must be signed in to change notification settings - Fork 0
/
bnssl.pas
66 lines (56 loc) · 1.87 KB
/
bnssl.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
unit sha512;
{$mode objfpc}{$H+}
interface
{$PACKRECORDS C}
type
tsha512context = record
state : array[0..7] of QWORD;
length : QWORD;
curlen : LongWord;
buf : array[0..127] of byte;
end;
tsha512digest = array [0..63] of byte;
function sha512init(out md:tsha512context):longint;
cdecl; external name 'sha512_init';
function sha512update(var md:tsha512context; const buf; buflen:LongWord):longint;
cdecl; external name 'sha512_update';
function sha512finalize(var md:tsha512context):longint;
cdecl; external name 'sha512_finalize';
function sha512final(var md:tsha512context; out digest):longint; overload;
function sha512final(var md:tsha512context; out digest; len:word):longint; overload;
function sha512initkey(out md:tsha512context; const key; keylen:LongWord; pad: byte): longint;
procedure SHA512Buffer(const Buf; BufLen: LongWord; out digest; digestlen:word );
implementation
{$L alg/sha512.o}
function sha512final(var md:tsha512context; out digest):longint;
begin
result:=sha512finalize(md);
Move( {source} md.state, {dest} digest, 64);
end;
function sha512final(var md:tsha512context; out digest; len:word):longint;
begin
result:=sha512finalize(md);
Move( {source} md.state, {dest} digest, len);
end;
function sha512initkey(out md:tsha512context; const key; keylen:LongWord; pad: byte): longint;
var blk: array [0..127] of byte;
var keyb: array [0..127] of byte absolute key;
var i:integer;
begin
result:=sha512init(md);
if keylen>sizeof(blk) then result:=1;
if result>0 then exit;
for i:=0 to keylen-1
do blk[i]:=keyb[i] xor pad;
for i:=keylen to high(blk)
do blk[i]:=pad;
result:=sha512update(md, blk, sizeof(blk));
end;
procedure SHA512Buffer(const Buf; BufLen: LongWord; out digest; digestlen:word );
var ctx:tSha512context;
begin
sha512init(ctx);
sha512update(ctx, buf, buflen);
sha512final(ctx,digest,digestlen);
end;
end.