📄 onguard.pas
字号:
TOgSpecialCode = class(TOgCodeBase)
function CheckCode(Report : Boolean) : TCodeStatus;
override;
function GetValue : LongInt;
{-return serial number (0 for error)}
published
{properties}
property Code
stored FStoreCode;
property StoreCode
default DefStoreCode;
end;
TOgUsageCode = class(TOgCodeBase)
protected {private}
{property variables}
FAutoDecrease : Boolean;
{event variables}
FOnChangeCode : TChangeCodeEvent;
protected
procedure Loaded;
override;
procedure DoOnChangeCode(Value : TCode);
dynamic;
public
constructor Create(AOwner : TComponent);
override;
function CheckCode(Report : Boolean) : TCodeStatus;
override;
procedure Decrease;
{-reduce number of uses and generate code}
function GetValue : LongInt;
{-return number of uses remaining}
published
{properties}
property AutoDecrease : Boolean
read FAutoDecrease
write FAutoDecrease
default DefAutoDecrease;
{events}
property OnChangeCode : TChangeCodeEvent
read FOnChangeCode
write FOnChangeCode;
end;
function GetCodeType(const Key : TKey; const Code : TCode) : TCodeType;
{-return the type of code}
function GetExpirationDate(const Key : TKey; const Code : TCode) : TDateTime;
{-return the date this code expires}
procedure InitDateCode(const Key : TKey; StartDate, EndDate : TDateTime; var Code : TCode);
function IsDateCodeValid(const Key : TKey; const Code : TCode) : Boolean;
function GetDateCodeValue(const Key : TKey; const Code : TCode) : TDateTime;
function IsDateCodeExpired(const Key : TKey; const Code : TCode) : Boolean;
procedure InitDaysCode(const Key : TKey; Days : Word; Expires : TDateTime; var Code : TCode);
function IsDaysCodeValid(const Key : TKey; const Code : TCode) : Boolean;
procedure DecDaysCode(const Key : TKey; var Code : TCode);
function GetDaysCodeValue(const Key : TKey; const Code : TCode) : LongInt;
function IsDaysCodeExpired(const Key : TKey; const Code : TCode) : Boolean;
procedure InitRegCode(const Key : TKey; const RegStr : string; Expires : TDateTime; var Code : TCode);
function IsRegCodeValid(const Key : TKey; const Code : TCode) : Boolean;
function IsRegCodeExpired(const Key : TKey; const Code : TCode) : Boolean;
procedure InitSerialNumberCode(const Key : TKey; Serial : LongInt; Expires : TDateTime; var Code : TCode);
function IsSerialNumberCodeValid(const Key : TKey; const Code : TCode) : Boolean;
function GetSerialNumberCodeValue(const Key : TKey; const Code : TCode) : LongInt;
function IsSerialNumberCodeExpired(const Key : TKey; const Code : TCode) : Boolean;
procedure InitSpecialCode(const Key : TKey; Value : LongInt; Expires : TDateTime; var Code : TCode);
function IsSpecialCodeValid(const Key : TKey; const Code : TCode) : Boolean;
function GetSpecialCodeValue(const Key : TKey; const Code : TCode) : LongInt;
function IsSpecialCodeExpired(const Key : TKey; const Code : TCode) : Boolean;
procedure InitUsageCode(const Key : TKey; Count : Word; Expires : TDateTime; var Code : TCode);
function IsUsageCodeValid(const Key : TKey; const Code : TCode) : Boolean;
procedure DecUsageCode(const Key : TKey; var Code : TCode);
function GetUsageCodeValue(const Key : TKey; const Code : TCode) : LongInt;
function IsUsageCodeExpired(const Key : TKey; const Code: TCode) : Boolean;
{generate key routines}
procedure GenerateRandomKeyPrim(var Key; KeySize : Cardinal);
procedure GenerateTMDKeyPrim(var Key; KeySize : Cardinal; const Str : string);
procedure GenerateMD5KeyPrim(var Key: TKey; const Str : string);
{modifier routines}
function CreateMachineID(MachineInfo : TEsMachineInfoSet) : LongInt; {!!.05}
function GenerateStringModifierPrim(const S : string) : LongInt;
function GenerateUniqueModifierPrim : LongInt;
function GenerateMachineModifierPrim : LongInt;
function GenerateDateModifierPrim(D : TDateTime) : LongInt;
procedure ApplyModifierToKeyPrim(Modifier : LongInt; var Key; KeySize : Cardinal);
{hash routines}
function StringHashElf(const Str : string) : LongInt;
{mixing routines}
procedure MixBlock(const Matrix : T128Bit; var Block; Encrypt : Boolean);
{utility routines}
function ExpandDate(D : Word) : TDateTime;
function ShrinkDate(D : TDateTime) : Word;
const
BaseDate : LongInt = 0;
implementation
uses
{$IFDEF TRIALRUN} OgTrial, {$ENDIF}
{$IFDEF VERSION3} ActiveX {$ELSE} OLE2 {$ENDIF} {!!.06}
{$IFNDEF NoMakeCodesSupport} , OnGuard2 {$ENDIF} {!!.10} {!!.10}
{$IFNDEF NoMakeKeysSupport} , OnGuard3 {$ENDIF} {!!.10} {!!.10}
;
{first 2048 bits of Pi in hexadecimal, low to high, without the leading "3"}
const
Pi2048: array [0..255] of Byte = (
$24, $3F, $6A, $88, $85, $A3, $08, $D3, $13, $19, $8A, $2E, $03, $70, $73, $44,
$A4, $09, $38, $22, $29, $9F, $31, $D0, $08, $2E, $FA, $98, $EC, $4E, $6C, $89,
$45, $28, $21, $E6, $38, $D0, $13, $77, $BE, $54, $66, $CF, $34, $E9, $0C, $6C,
$C0, $AC, $29, $B7, $C9, $7C, $50, $DD, $3F, $84, $D5, $B5, $B5, $47, $09, $17,
$92, $16, $D5, $D9, $89, $79, $FB, $1B, $D1, $31, $0B, $A6, $98, $DF, $B5, $AC,
$2F, $FD, $72, $DB, $D0, $1A, $DF, $B7, $B8, $E1, $AF, $ED, $6A, $26, $7E, $96,
$BA, $7C, $90, $45, $F1, $2C, $7F, $99, $24, $A1, $99, $47, $B3, $91, $6C, $F7,
$08, $01, $F2, $E2, $85, $8E, $FC, $16, $63, $69, $20, $D8, $71, $57, $4E, $69,
$A4, $58, $FE, $A3, $F4, $93, $3D, $7E, $0D, $95, $74, $8F, $72, $8E, $B6, $58,
$71, $8B, $CD, $58, $82, $15, $4A, $EE, $7B, $54, $A4, $1D, $C2, $5A, $59, $B5,
$9C, $30, $D5, $39, $2A, $F2, $60, $13, $C5, $D1, $B0, $23, $28, $60, $85, $F0,
$CA, $41, $79, $18, $B8, $DB, $38, $EF, $8E, $79, $DC, $B0, $60, $3A, $18, $0E,
$6C, $9E, $0E, $8B, $B0, $1E, $8A, $3E, $D7, $15, $77, $C1, $BD, $31, $4B, $27,
$78, $AF, $2F, $DA, $55, $60, $5C, $60, $E6, $55, $25, $F3, $AA, $55, $AB, $94,
$57, $48, $98, $62, $63, $E8, $14, $40, $55, $CA, $39, $6A, $2A, $AB, $10, $B6,
$B4, $CC, $5C, $34, $11, $41, $E8, $CE, $A1, $54, $86, $AF, $7C, $72, $E9, $93);
{mixing routines}
procedure Mix128(var X : T128Bit);
var
AA, BB, CC, DD : LongInt;
begin
AA := X[0]; BB := X[1]; CC := X[2]; DD := X[3];
AA := AA + DD; DD := DD + AA; AA := AA xor (AA shr 7);
BB := BB + AA; AA := AA + BB; BB := BB xor (BB shl 13);
CC := CC + BB; BB := BB + CC; CC := CC xor (CC shr 17);
DD := DD + CC; CC := CC + DD; DD := DD xor (DD shl 9);
AA := AA + DD; DD := DD + AA; AA := AA xor (AA shr 3);
BB := BB + AA; AA := AA + BB; BB := BB xor (BB shl 7);
CC := CC + BB; BB := BB + CC; CC := CC xor (DD shr 15);
DD := DD + CC; CC := CC + DD; DD := DD xor (DD shl 11);
X[0] := AA; X[1] := BB; X[2] := CC; X[3] := DD;
end;
{quick (block) mixer routine}
procedure MixBlock(const Matrix : T128bit; var Block; Encrypt : Boolean);
const
CKeyBox : array [False..True, 0..3, 0..2] of LongInt =
(((0, 3, 1), (2, 1, 3), (1, 0, 2), (3, 2, 0)),
((3, 2, 0), (1, 0, 2), (2, 1, 3), (0, 3, 1)));
var
Blocks : array [0..1] of LongInt absolute Block;
Work : LongInt;
Right : LongInt;
Left : LongInt;
R : LongInt;
AA, BB : LongInt;
CC, DD : LongInt;
begin
Right := Blocks[0];
Left := Blocks[1];
for R := 0 to 3 do begin
{transform the right side}
AA := Right;
BB := Matrix[CKeyBox[Encrypt, R, 0]];
CC := Matrix[CKeyBox[Encrypt, R, 1]];
DD := Matrix[CKeyBox[Encrypt, R, 2]];
{commented code does not affect results - removed for speed}
AA := AA + DD; DD := DD + AA; AA := AA xor (AA shr 7);
BB := BB + AA; AA := AA + BB; BB := BB xor (BB shl 13);
CC := CC + BB; BB := BB + CC; CC := CC xor (CC shr 17);
DD := DD + CC; CC := CC + DD; DD := DD xor (DD shl 9);
AA := AA + DD; DD := DD + AA; AA := AA xor (AA shr 3);
BB := BB + AA; {AA := AA + BB;} BB := BB xor (BB shl 7);
CC := CC + BB; {BB := BB + CC;} CC := CC xor (DD shr 15);
DD := DD + CC; {CC := CC + DD;} DD := DD xor (DD shl 11);
Work := Left xor DD;
Left := Right;
Right := Work;
end;
Blocks[0] := Left;
Blocks[1] := Right;
end;
function HashElf(const Buf; BufSize : LongInt) : LongInt;
var
Bytes : TByteArray absolute Buf;
I, X : LongInt;
begin
Result := 0;
for I := 0 to BufSize - 1 do begin
Result := (Result shl 4) + Bytes[I];
X := Result and $F0000000;
if (X <> 0) then
Result := Result xor (X shr 24);
Result := Result and (not X);
end;
end;
function StringHashElf(const Str : string) : LongInt;
begin
Result := HashElf(Str[1], Length(Str));
end;
{internal routines for MD5}
type
TMD5ContextEx = record
Count : array [0..1] of DWord; {number of bits handled mod 2^64}
State : array [0..3] of DWord; {scratch buffer}
Buf : array [0..63] of Byte; {input buffer}
end;
{MD5 routines}
{$IFDEF Win32}
function RolX(I, C : DWord) : DWord; register; {!!.07}
asm
mov ecx, edx {get count to cl}
rol eax, cl {rotate eax by cl}
end;
{$ELSE}
function RolX(I, C : DWord) : DWord; assembler; {!!.07}
asm
db $66
mov ax,word ptr I {eax = I}
db $66
mov cx,word ptr C {ecx = C}
db $66
rol ax, cl {rotate eax by cl}
db $66
push ax {push eax}
{set result}
pop ax {low word to ax}
pop dx {high word to dx}
end;
{$ENDIF}
{!!.07}
procedure Transform(var Buffer : array of DWord; const InBuf : array of DWord);
const
S11 = 7;
S12 = 12;
S13 = 17;
S14 = 22;
S21 = 5;
S22 = 9;
S23 = 14;
S24 = 20;
S31 = 4;
S32 = 11;
S33 = 16;
S34 = 23;
S41 = 6;
S42 = 10;
S43 = 15;
S44 = 21;
var
Buf : array [0..3] of DWord absolute Buffer;
InA : array [0..15] of DWord absolute InBuf;
A : DWord;
B : DWord;
C : DWord;
D : DWord;
procedure FF(var A : DWord; B, C, D, X, S, AC : DWord);
begin
A := RolX(A + ((B and C) or (not B and D)) + X + AC, S) + B;
end;
procedure GG(var A : DWord; B, C, D, X, S, AC : DWord);
begin
A := RolX(A + ((B and D) or (C and not D)) + X + AC, S) + B;
end;
procedure HH(var A : DWord; B, C, D, X, S, AC : DWord);
begin
A := RolX(A + (B xor C xor D) + X + AC, S) + B;
end;
procedure II(var A : DWord; B, C, D, X, S, AC : DWord);
begin
A := RolX(A + (C xor (B or not D)) + X + AC, S) + B;
end;
begin
A := Buf [0];
B := Buf [1];
C := Buf [2];
D := Buf [3];
{round 1}
FF(A, B, C, D, InA [ 0], S11, $D76AA478); { 1 }
FF(D, A, B, C, InA [ 1], S12, $E8C7B756); { 2 }
FF(C, D, A, B, InA [ 2], S13, $242070DB); { 3 }
FF(B, C, D, A, InA [ 3], S14, $C1BDCEEE); { 4 }
FF(A, B, C, D, InA [ 4], S11, $F57C0FAF); { 5 }
FF(D, A, B, C, InA [ 5], S12, $4787C62A); { 6 }
FF(C, D, A, B, InA [ 6], S13, $A8304613); { 7 }
FF(B, C, D, A, InA [ 7], S14, $FD469501); { 8 }
FF(A, B, C, D, InA [ 8], S11, $698098D8); { 9 }
FF(D, A, B, C, InA [ 9], S12, $8B44F7AF); { 10 }
FF(C, D, A, B, InA [10], S13, $FFFF5BB1); { 11 }
FF(B, C, D, A, InA [11], S14, $895CD7BE); { 12 }
FF(A, B, C, D, InA [12], S11, $6B901122); { 13 }
FF(D, A, B, C, InA [13], S12, $FD987193); { 14 }
FF(C, D, A, B, InA [14], S13, $A679438E); { 15 }
FF(B, C, D, A, InA [15], S14, $49B40821); { 16 }
{round 2}
GG(A, B, C, D, InA [ 1], S21, $F61E2562); { 17 }
GG(D, A, B, C, InA [ 6], S22, $C040B340); { 18 }
GG(C, D, A, B, InA [11], S23, $265E5A51); { 19 }
GG(B, C, D, A, InA [ 0], S24, $E9B6C7AA); { 20 }
GG(A, B, C, D, InA [ 5], S21, $D62F105D); { 21 }
GG(D, A, B, C, InA [10], S22, $02441453); { 22 }
GG(C, D, A, B, InA [15], S23, $D8A1E681); { 23 }
GG(B, C, D, A, InA [ 4], S24, $E7D3FBC8); { 24 }
GG(A, B, C, D, InA [ 9], S21, $21E1CDE6); { 25 }
GG(D, A, B, C, InA [14], S22, $C33707D6); { 26 }
GG(C, D, A, B, InA [ 3], S23, $F4D50D87); { 27 }
GG(B, C, D, A, InA [ 8], S24, $455A14ED); { 28 }
GG(A, B, C, D, InA [13], S21, $A9E3E905); { 29 }
GG(D, A, B, C, InA [ 2], S22, $FCEFA3F8); { 30 }
GG(C, D, A, B, InA [ 7], S23, $676F02D9); { 31 }
GG(B, C, D, A, InA [12], S24, $8D2A4C8A); { 32 }
{round 3}
HH(A, B, C, D, InA [ 5], S31, $FFFA3942); { 33 }
HH(D, A, B, C, InA [ 8], S32, $8771F681); { 34 }
HH(C, D, A, B, InA [11], S33, $6D9D6122); { 35 }
HH(B, C, D, A, InA [14], S34, $FDE5380C); { 36 }
HH(A, B, C, D, InA [ 1], S31, $A4BEEA44); { 37 }
HH(D, A, B, C, InA [ 4], S32, $4BDECFA9); { 38 }
HH(C, D, A, B, InA [ 7], S33, $F6BB4B60); { 39 }
HH(B, C, D, A, InA [10], S34, $BEBFBC70); { 40 }
HH(A, B, C, D, InA [13], S31, $289B7EC6); { 41 }
HH(D, A, B, C, InA [ 0], S32, $EAA127FA); { 42 }
HH(C, D, A, B, InA [ 3], S33, $D4EF3085); { 43 }
HH(B, C, D, A, InA [ 6], S34, $4881D05); { 44 }
HH(A, B, C, D, InA [ 9], S31, $D9D4D039); { 45 }
HH(D, A, B, C, InA [12], S32, $E6DB99E5); { 46 }
HH(C, D, A, B, InA [15], S33, $1FA27CF8); { 47 }
HH(B, C, D, A, InA [ 2], S34, $C4AC5665); { 48 }
{round 4}
II(A, B, C, D, InA [ 0], S41, $F4292244); { 49 }
II(D, A, B, C, InA [ 7], S42, $432AFF97); { 50 }
II(C, D, A, B, InA [14], S43, $AB9423A7); { 51 }
II(B, C, D, A, InA [ 5], S44, $FC93A039); { 52 }
II(A, B, C, D, InA [12], S41, $655B59C3); { 53 }
II(D, A, B, C, InA [ 3], S42, $8F0CCC92); { 54 }
II(C, D, A, B, InA [10], S43, $FFEFF47D); { 55 }
II(B, C, D, A, InA [ 1], S44, $85845DD1); { 56 }
II(A, B, C, D, InA [ 8], S41, $6FA87E4F); { 57 }
II(D, A, B, C, InA [15], S42, $FE2CE6E0); { 58 }
II(C, D, A, B, InA [ 6], S43, $A3014314); { 59 }
II(B, C, D, A, InA [13], S44, $4E0811A1); { 60 }
II(A, B, C, D, InA [ 4], S41, $F7537E82); { 61 }
II(D, A, B, C, InA [11], S42, $BD3AF235); { 62 }
II(C, D, A, B, InA [ 2], S43, $2AD7D2BB); { 63 }
II(B, C, D, A, InA [ 9], S44, $EB86D391); { 64 }
Inc(Buf [0], A);
Inc(Buf [1], B);
Inc(Buf [2], C);
Inc(Buf [3], D);
end;
procedure InitMD5(var Context : TMD5Context);
var
MD5 : TMD5ContextEx absolute Context;
begin
MD5.Count[0] := 0;
MD5.Count[1] := 0;
{load magic initialization constants}
MD5.State[0] := $67452301;
MD5.State[1] := $EFCDAB89;
MD5.State[2] := $98BADCFE;
MD5.State[3] := $10325476;
end;
procedure UpdateMD5(var Context : TMD5Context; const Buf; BufSize : LongInt);
var
MD5 : TMD5ContextEx absolute Context;
Bytes : TByteArray absolute Buf;
InBuf : array [0..15] of DWord; {!!.07}
BufOfs : LongInt;
MDI : Word;
I : Word;
II : Word;
begin
{compute number of bytes mod 64}
MDI := (MD5.Count[0] shr 3) and $3F;
{update number of bits}
if ((MD5.Count[0] + (DWord(BufSize) shl 3)) < MD5.Count[0]) then
Inc(MD5.Count[1]);
Inc(MD5.Count[0], BufSize shl 3);
Inc(MD5.Count[1], BufSize shr 29);
{add new byte acters to buffer}
BufOfs := 0;
while (BufSize > 0) do begin
Dec(BufSize);
MD5.Buf[MDI] := Bytes[BufOfs];
Inc(MDI);
Inc(BufOfs);
if (MDI = $40) then begin
II := 0;
for I := 0 to 15 do begin
InBuf[I] := LongInt(MD5.Buf[II + 3]) shl 24 or
LongInt(MD5.Buf[II + 2]) shl 16 or
LongInt(MD5.Buf[II + 1]) shl 8 or
LongInt(MD5.Buf[II]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -