-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSFFS.AppKey.pas
74 lines (61 loc) · 1.35 KB
/
SFFS.AppKey.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
67
68
69
70
71
72
73
74
unit SFFS.AppKey;
interface
uses
System.SysUtils;
const
APPKEY_LEN = 8;
type
TAppKey = packed record
public
// key like '1122334455667788'
class operator Implicit(const a: string): TAppKey; static;
class operator Implicit(const a: TAppKey): string; static;
procedure Clear; inline;
function IsNull: boolean; inline;
procedure &Xor(const Buffer; Size: integer);
private
case byte of
0:
(bytes: array [0 .. APPKEY_LEN - 1] of byte);
1:
(u64: uint64);
end;
implementation
procedure TAppKey.&Xor(const Buffer; Size: integer);
var
i: integer;
p: PByte;
begin
p := @Buffer;
for i := 0 to Size - 1 do
begin
p^ := p^ xor bytes[i mod APPKEY_LEN];
inc(p);
end;
end;
procedure TAppKey.Clear;
begin
FillChar(Self.bytes[0], APPKEY_LEN, 0);
end;
class operator TAppKey.Implicit(const a: string): TAppKey;
var
i: integer;
begin
if a.Length <> 2 * Length(result.bytes) then
raise Exception.Create('Invalid APPKEY string length');
for i := low(result.bytes) to high(result.bytes) do
result.bytes[i] := ('$' + a.Substring(i * 2, 2)).ToInteger;
end;
class operator TAppKey.Implicit(const a: TAppKey): string;
var
b: byte;
begin
result := '';
for b in a.bytes do
result := result + IntToHex(b, 2);
end;
function TAppKey.IsNull: boolean;
begin
result := Self.u64 = 0;
end;
end.