-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpOps.pas
48 lines (41 loc) · 1.17 KB
/
ExpOps.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
unit ExpOps;
INTERFACE
uses MemStream,NetAddr,ed25519,Sha512;
{$I Mutable-file.pas}
procedure UpdateMutSig(var mf:file; sec:tKey64);
procedure UpdateMutSig(var mf:file; const sec:tKey64; const pub:tKey32);
{function VerifyMutSig(var ctx:tSha512Context; const hdr:tMutHdr):boolean;
just ed25519.verify2}
IMPLEMENTATION
uses SysUtils;
procedure UpdateMutSig(var mf:file; sec:tKey64);
var pub:tKey32;
begin
CreateKeyPair(pub,sec);
UpdateMutSig(mf,sec,pub);
end;
procedure UpdateMutSig(var mf:file; const sec:tKey64; const pub:tKey32);
var nDay:LongWord;
var hdr:tMutHdr;
var bufr,buf:pointer;
var datasize:integer;
begin
{$I+}
BlockRead(mf,hdr,sizeof(hdr));
if CompareByte(hdr.Magic,cMutHdrMagic,sizeof(hdr.Magic)) <> 0 then begin
raise eFormatError.create('Invalid magic sequence');
end;
nDay:=trunc(Now-cMutEpoch);
hdr.pub:=Pub;
hdr.Ver:=LongWord(hdr.Ver)+1;
hdr.Day:=nDay;
datasize:=FileSize(mf)-sizeof(hdr);
bufr:=GetMem(datasize+64); {first 64B of header are signed}
Move(hdr,bufr^,64);
buf:=bufr+64;
BlockRead(mf,buf^,datasize);
ed25519.Sign(hdr.Sig, bufr^, datasize+64, pub, sec);
Seek(mf,0);
BlockWrite(mf,hdr,sizeof(hdr));
end;
END.