This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
nosocrypto.pas
1001 lines (934 loc) · 27.6 KB
/
nosocrypto.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
UNIT nosocrypto;
{
Unit nosocrypto 1.3
December 4th, 2023
Noso Unit for crypto functions
Requires: cryptohashlib , mpsignerutils
}
{$mode ObjFPC}{$H+}
INTERFACE
uses
Classes, SysUtils, strutils,
HlpHashFactory, md5,
ClpConverters,ClpBigInteger,SbpBase58,
mpsignerutils, base64, NosoDebug, nosogeneral;
Type
DivResult = packed record
cociente : string[255];
residuo : string[255];
end;
{Hashing functions}
function HashSha256String(StringToHash:string):string;
function HashMD160String(StringToHash:string):String;
Function HashMD5String(StringToHash:String):String;
Function HashMD5File(FileToHash:String):String;
{General functions}
Function IsValid58(base58text:string):boolean;
Function GetStringSigned(StringtoSign, PrivateKey:String):String;
Function VerifySignedString(StringToVerify,B64String,PublicKey:String):boolean;
function GetAddressFromPublicKey(PubKey:String;AddType : integer = 0):String;
function NewGetAddressFromPublicKey(PubKey:String):String;
function FutureGetAddressFromPublicKey(const PubKey: String): String;
Function GenerateNewAddress(out pubkey:String;out privkey:String):String;
Function IsValidHashAddress(Address:String):boolean;
Function GetTransferHash(TextLine:string):String;
function GetOrderHash(TextLine:string):String;
Function GetCertificateChecksum(certificate:String):String;
Function GetCertificate(Pubkey,privkey,currtime:string):string;
Function CheckCertificate(certificate:string;out TimeStamp:String):string;
Function CheckHashDiff(Target,ThisHash:String):string;
Function GetMultiSource(Source: String;AddsTotal:integer;Out OrderedSource:String) : boolean;
{New base conversion functions}
function B10ToB16(const sVal: String): String;
Function B10ToB58(const sVal: String): String;
Function B16ToB10(const sHex: String): String;
Function B16ToB36(const sHex: String): String;
Function B16ToB58(const sHex: String): String;
Function B16ToB58Lite1_60(const sHex: String): String;
Function B58ToB10(const sVal: String): String;
Function B58ToB16(const sVal: String): String;
Function ChecksumBase58(const S: String): integer;
Function BMB58resumenNew(numero58:string):string;
Function BMB58resumen(numero58:string):string;
Function BMB58resumenInt(numero58:string):integer;
// Big Maths
function ClearLeadingCeros(numero:string):string;
function BMAdicion(numero1,numero2:string):string;
Function PonerCeros(numero:String;cuantos:integer):string;
Function BMMultiplicar(Numero1,Numero2:string):string;
Function BMDividir(Numero1,Numero2:string):DivResult;
Function BMExponente(Numero1,Numero2:string):string;
function BMHexToDec(numerohex:string):string;
Function BM58ToDec(number58:string):String;
function BMHexTo58(numerohex:string;alphabetnumber:integer):string;
function BMDecTo58(numero:string):string;
function BMDecToHex(numero:string):string;
Const
HexAlphabet : string = '0123456789ABCDEF';
B36Alphabet : string = '0123456789abcdefghijklmnopqrstuvwxyz';
B58Alphabet : string = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
AddtypeReg = 0;
AddTypeMulti = 1;
IMPLEMENTATION
{$REGION Internal utility methods}
function GetAlphabet(const Base: Byte): String; {$ifopt D-}inline;{$endif}
begin
case Base of
36: Result := B36Alphabet;
58: Result := B58Alphabet;
else Result := '';
end;
end;
function IsValidInput(const S: String): Boolean; {$ifopt D-}inline;{$endif}
begin
Result := (Length(S) <> 0) and (S <> '0');
end;
function EncodeBaseN(const bytes: TBytes; const sAlphabet: String; iBase: Int32 = 0): String;
const
growthPercentage = Int32(154);
var
bytesLen, numZeroes, outputLen, Length, carry, i, resultLen: Int32;
inputPtr, pInput, pEnd, outputPtr, pOutputEnd, pDigit, pOutput: PByte;
alphabetPtr, resultPtr, pResult: PChar;
ZeroChar: Char;
output: TBytes;
Value: String;
begin
result := '';
bytesLen := System.Length(bytes);
if (bytesLen = 0) then
begin
Exit;
end;
inputPtr := PByte(bytes);
Value := sAlphabet;
alphabetPtr := PChar(Value);
pInput := inputPtr;
pEnd := inputPtr + bytesLen;
while ((pInput <> pEnd) and (pInput^ = 0)) do
begin
System.Inc(pInput);
end;
numZeroes := Int32(pInput - inputPtr);
ZeroChar := alphabetPtr^;
if (pInput = pEnd) then
begin
result := StringOfChar(ZeroChar, numZeroes);
Exit;
end;
outputLen := bytesLen * growthPercentage div 100 + 1;
Length := 0;
System.SetLength(output, outputLen);
outputPtr := PByte(output);
pOutputEnd := outputPtr + outputLen - 1;
while (pInput <> pEnd) do
begin
carry := pInput^;
i := 0;
pDigit := pOutputEnd;
while (((carry <> 0) or (i < Length)) and (pDigit >= outputPtr)) do
begin
carry := carry + (256 * pDigit^);
pDigit^ := Byte(carry mod iBase);
carry := carry div iBase;
System.Dec(pDigit);
System.Inc(i);
end;
Length := i;
System.Inc(pInput);
end;
System.Inc(pOutputEnd);
pOutput := outputPtr;
while ((pOutput <> pOutputEnd) and (pOutput^ = 0)) do
begin
System.Inc(pOutput);
end;
resultLen := {numZeroes +} Int32(pOutputEnd - pOutput);
result := StringOfChar(ZeroChar, resultLen);
resultPtr := PChar(result);
pResult := resultPtr {+ numZeroes};
while (pOutput <> pOutputEnd) do
begin
pResult^ := alphabetPtr[pOutput^];
System.Inc(pOutput);
System.Inc(pResult);
end;
end;
{$ENDREGION}
{Returns the hash Sha2-256 of a string}
Function HashSha256String(StringToHash:string):string;
Begin
result := THashFactory.TCrypto.CreateSHA2_256().ComputeString(StringToHash, TEncoding.UTF8).ToString();
End;
{Returns the hash RIPMED-160 of a string}
Function HashMD160String(StringToHash:string):String;
Begin
result := THashFactory.TCrypto.CreateRIPEMD160().ComputeString(StringToHash, TEncoding.UTF8).ToString();
End;
{Returns the hash MD5 of a string}
Function HashMD5String(StringToHash:String):String;
Begin
result := Uppercase(MD5Print(MD5String(StringToHash)));
End;
{Returns the hash MD5 of a file on disk}
Function HashMD5File(FileToHash:String):String;
Begin
result := Uppercase('d41d8cd98f00b204e9800998ecf8427e');
TRY
result := UpperCase(MD5Print(MD5File(FileToHash)));
EXCEPT ON E:Exception do
END;
End;
{Verify if a string is a valid Base58 one}
function IsValid58(base58text:string):boolean;
var
counter : integer;
Begin
result := true;
if Length(base58text) = 0 then Exit(False);
for counter := 1 to length(base58text) do
if pos (base58text[counter],B58Alphabet) = 0 then
Exit(false);
End;
{Signs a message with the given privatekey}
Function GetStringSigned(StringtoSign, PrivateKey:String):String;
var
Signature, MessageAsBytes: TBytes;
Begin
Result := '';
TRY
MessageAsBytes :=StrToByte(DecodeStringBase64(StringtoSign));
Signature := TSignerUtils.SignMessage(MessageAsBytes, StrToByte(DecodeStringBase64(PrivateKey)),TKeyType.SECP256K1);
Result := EncodeStringBase64(ByteToString(Signature));
EXCEPT Exit;
END{Try};
End;
{Verify if a signed message is valid}
Function VerifySignedString(StringToVerify,B64String,PublicKey:String):boolean;
var
Signature, MessageAsBytes: TBytes;
Begin
result := false;
TRY
MessageAsBytes := StrToByte(DecodeStringBase64(StringToVerify));
Signature := StrToByte(DecodeStringBase64(B64String));
Result := TSignerUtils.VerifySignature(Signature, MessageAsBytes,StrToByte(DecodeStringBase64(PublicKey)), TKeyType.SECP256K1);
EXCEPT Exit;
END{Try};
End;
// Generates the public hash from the public key
function GetAddressFromPublicKey(PubKey:String;AddType : integer = 0):String;
var
PubSHAHashed,Hash1,Hash2,clave:String;
sumatoria : string;
Begin
PubSHAHashed := HashSha256String(PubKey);
Hash1 := HashMD160String(PubSHAHashed);
hash1 := BMHexTo58(Hash1,58);
sumatoria := BMB58resumen(Hash1);
clave := BMDecTo58(sumatoria);
hash2 := hash1+clave;
if AddType = 0 then Result := 'N'+hash2
else if Addtype = 1 then Result := 'M'+Hash2;
End;
function NewGetAddressFromPublicKey(PubKey:String):String;
var
PubSHAHashed,Hash1,Hash2,clave:String;
sumatoria : string;
Begin
PubSHAHashed := HashSha256String(PubKey);
Hash1 := HashMD160String(PubSHAHashed);
hash1 := B16toB58(Hash1);
sumatoria := BMB58resumen(Hash1);
clave := B10toB58(sumatoria);
hash2 := hash1+clave;
Result := 'N'+hash2;
End;
function FutureGetAddressFromPublicKey(const PubKey: String): String;
var
s_data, s_cksum, s_cksum_hex: AnsiString;
hashSHA256: String;
hashRMD160: TBytes;
begin
Result := EmptyStr;
if PubKey.IsEmpty then
Exit;
{ SHA256 PubKey string hash }
hashSHA256 := THashFactory.TCrypto.CreateSHA2_256
.ComputeString(PubKey, TEncoding.ANSI)
.ToString;
{ RIPEMD160 hash of SHA256 PubKey hash }
hashRMD160 := THashFactory.TCrypto.CreateRIPEMD160
.ComputeString(hashSHA256, TEncoding.ANSI)
.GetBytes;
// Quitar ceros al string aqui
{ Encode RIPEMD160 hash as Base58 string }
s_data := TBase58.BitCoin.Encode(hashRMD160);
if s_data[1]='1' then delete(s_Data,1,1);
{ Get s_data checksum in HEX }
s_cksum_hex := HexStr(ChecksumBase58(s_data), 4);
{ Encode checksum as Base58 string }
s_cksum := TBase58.BitCoin.Encode(
TConverters.ConvertHexStringToBytes(s_cksum_hex)
);
{ Concat all }
Result := Concat('N', s_data, s_cksum);
end;
{Generates a new keys pair and returns the hash}
Function GenerateNewAddress(out pubkey:String;out privkey:String):String;
var
KeysPair : TKeyPair;
IsDone : boolean = false;
HashAdd : String;
Begin
Result := '';
Repeat
KeysPair := TSignerUtils.GenerateECKeyPair(TKeyType.SECP256K1);
HashAdd := GetAddressFromPublicKey(Keyspair.PublicKey);
if length(HashAdd) >= 20 then
begin
pubkey := Keyspair.PublicKey;
PrivKey := KeysPair.PrivateKey;
Result := HashAdd;
IsDone := true;
end;
until IsDone;
End;
{Checks if a string is a valid address hash}
Function IsValidHashAddress(Address:String):boolean;
var
OrigHash : String;
FirstChar : string;
Begin
result := false;
if length(Address) < 1 then exit;
FirstChar := address[1];
if ((length(address)>20) and ((address[1] = 'N') or (address[1] = 'M')) ) then
begin
OrigHash := Copy(Address,2,length(address)-3);
if IsValid58(OrigHash) then
if FirstChar+OrigHash+(B10toB58(BMB58resumen(OrigHash))) = Address then
result := true;
end
End;
{Returns a transfer hash, base58}
Function GetTransferHash(TextLine:string):String;
var
Resultado : String = '';
Begin
Resultado := HashSHA256String(TextLine);
Resultado := B16toB58(Resultado);
Result := 'tR'+Resultado+ B10toB58(BMB58resumen(Resultado)) ;
End;
{Returns the Order hash, base36}
function GetOrderHash(TextLine:string):String;
Begin
Result := HashSHA256String(TextLine);
Result := 'OR'+B16ToB36(Result);
End;
{Returns the Address certificate for the submitted data}
Function GetCertificate(Pubkey,privkey,currtime:string):string;
var
Certificate : String;
Address : String;
Signature : String;
Checksum : String;
Begin
Result := '';
TRY
Address := GetAddressFromPublicKey(Pubkey);
Signature := GetStringSigned('OWN'+Address+currtime,PrivKey);
Certificate := 'OWN:'+Pubkey+':'+Currtime+':'+signature;
//ToLog('console','To encode: '+certificate);
Certificate := UPPERCASE(XorEncode(HashSha256String('noso'),certificate));
//ToLog('console','Encoded: '+certificate);
result := B16ToB58('1'+Certificate);
Checksum := GetCertificateChecksum(Result);
//ToLog('console','Checksum: '+Checksum);
Result := Result+Checksum;
EXCEPT Exit;
END; {TRY}
End;
{Verify if a given certificate is valid and returns the address and timestamp}
Function CheckCertificate(certificate:string;out TimeStamp:String):string;
var
DataArray : array of string;
Address : String;
CertTime : String;
Signature : String;
CheckSum : string;
Begin
Result := '';
TRY
Checksum := copy(Certificate,length(certificate)-2,3);
Certificate := copy(Certificate,1,length(certificate)-3);
if CheckSum <> GetCertificateChecksum(Certificate) then exit;
Certificate := B58toB16(certificate);
Certificate := copy(Certificate,2,length(certificate));
//ToLog('console','To decode: '+certificate);
Certificate := XorDecode(HashSha256String('noso'), Certificate);
//ToLog('console','Decoded: '+certificate);
DataArray := SplitString(Certificate,':');
Address := GetAddressFromPublicKey(DataArray[1]);
CertTime := DataArray[2];
Signature := DataArray[3];
if VerifySignedString('OWN'+Address+CertTime,Signature,DataArray[1]) then
{Verified certificate}
begin
TimeStamp := CertTime;
Result := Address;
end;
EXCEPT Exit;
END; {TRY}
End;
{Verify the difference between MD5 hashes}
Function CheckHashDiff(Target,ThisHash:String):string;
var
counter : integer;
ValA, ValB, Diference : Integer;
ResChar : String;
Resultado : String = '';
Begin
result := 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
if Length(Target) < 32 then SetLength(Target,32);
if Length(ThisHash) < 32 then SetLength(ThisHash,32);
for counter := 1 to 32 do
begin
ValA := Hex2Dec(ThisHash[counter]);
ValB := Hex2Dec(Target[counter]);
Diference := Abs(ValA - ValB);
ResChar := UPPERCASE(IntToHex(Diference,1));
Resultado := Resultado+ResChar
end;
Result := Resultado;
End;
//getmulti 1,2 N31ThXmSR2gua5tfCFNLEkYK9yNQsDi,N31ThXmSR2gua5tfCFNLEkYK9yNQsDi
Function GetMultiSource(Source: String;AddsTotal:integer;Out OrderedSource:String) : boolean;
var
Addresses : array of string;
Counter : integer = 0;
Counter2 : integer = 0;
ThisSour : string;
Inserted : boolean = false;
LocSource : string;
Existing : string = '';
Begin
Result := false;
OrderedSource := '';
LocSource := StringReplace(Source,',',' ',[rfReplaceAll, rfIgnoreCase]);
SetLength(Addresses,0);
Repeat
Inserted := false;
ThisSour := Parameter(LocSource,counter);
if thisSour <> '' then
begin
if AnsiContainsStr(Existing,ThisSour) then
begin
OrderedSource:= 'Duplicated address -> '+ThisSour;
Exit;
end;
Existing := Existing+','+thisSour;
if not IsValidHashAddress(ThisSour) then
begin
OrderedSource:= 'Invalid hash address -> '+ThisSour;
Exit;
end;
if length(Addresses) = 0 then Insert(ThisSour,Addresses,0)
else
begin
For counter2 := 0 to length(addresses)-1 do
begin
if ThisSour < Addresses[counter2] then
begin
Insert(ThisSour,Addresses,counter2);
Inserted := true;
end;
end;
if not inserted then Insert(ThisSour,Addresses,length(Addresses));
end;
end;
Inc(Counter);
until ( (ThisSour = '') or (Counter = AddsTotal) );
if length(Addresses) <> AddsTotal then OrderedSource:= format('Wrong addresses count %d/%d',[length(addresses),AddsTotal]);
if OrderedSource = '' then
begin
for counter := 0 to length(Addresses)-1 do
Begin
OrderedSource := OrderedSource+Addresses[counter];
if counter < LEngth(addresses)-1 then OrderedSource := OrderedSource+',';
end;
Result := true;
end;
End;
{** New base conversion functions **}
Function TrimLeadingCeros(const S: String): String;
Begin
Result := S.Trim;
if Result[1] = '0' then
Result := Result.TrimLeft('0');
End;
function B10ToB16(const sVal: String): String;
var
S: String;
Begin
Result := '0';
S := sVal.Trim;
if (Length(S) = 0) then
Exit;
try
Result := TConverters.ConvertBytesToHexString(TBigInteger.Create(S).ToByteArrayUnsigned,False);
Result := TrimLeadingCeros(Result);
except
Exit; { convert or Parser Errors }
end;
End;
Function B10ToB58(const sVal: String): String;
var
S: String;
Begin
Result := '1';
S := sVal.Trim;
if (Length(S) = 0) then
Exit;
Result := TBase58.BitCoin.Encode(TBigInteger.Create(S).ToByteArrayUnsigned);
End;
Function B16ToB10(const sHex: String): String;
var
bytes: TBytes;
S: String;
Begin
Result := '0';
S := sHex.Trim;
if (Length(S) = 0) then
Exit;
if (Length(S) mod 2) <> 0 then
S := Concat('0', S);
try
bytes := TConverters.ConvertHexStringToBytes(S);
except
Exit { invalid HEX input }
end;
Result := TBigInteger.Create(1, bytes).ToString;
End;
Function B16ToB36(const sHex: String): String;
const
baseLength = 36;
var
bytes: TBytes;
S: String;
Begin
Result := '';
S := sHex.Trim;
if not IsValidInput(S) then
Exit('0');
if (Length(S) mod 2) <> 0 then
S := Concat('0', S);
try
bytes := TConverters.ConvertHexStringToBytes(S);
except
Exit('0') { invalid HEX input }
end;
Result := EncodeBaseN(bytes, GetAlphabet(baseLength), baseLength);
End;
Function B16ToB58(const sHex: String): String;
var
bytes: TBytes;
S: String;
Begin
Result := '1';
S := sHex.Trim;
if (Length(S) = 0) then
Exit;
if (Length(S) mod 2) <> 0 then
S := Concat('0', S);
try
bytes := TConverters.ConvertHexStringToBytes(S);
except
Exit { invalid HEX input }
end;
Result := TBase58.BitCoin.Encode(TBigInteger.Create(bytes).ToByteArrayUnsigned);
End;
Function B16ToB58Lite1_60(const sHex: String): String;
var
bytes: TBytes;
S: String;
Begin
Result := '1';
S := sHex.Trim;
if (Length(S) = 0) then
Exit;
if (Length(S) mod 2) <> 0 then
S := Concat('0', S);
try
bytes := TConverters.ConvertHexStringToBytes(S);
except
Exit { invalid HEX input }
end;
Result := TBase58.BitCoin.Encode(TBigInteger.Create(bytes).ToByteArrayUnsigned);
End;
Function B58ToB10(const sVal: String): String;
var
bytes: TBytes;
S: String;
Begin
Result := '0';
S := sVal.Trim;
if (Length(S) = 0) then
Exit;
try
bytes := TBase58.BitCoin.Decode(S);
except Exit
end;
Result := TBigInteger.Create(1, bytes).ToString;
End;
Function B58ToB16(const sVal: String): String;
var
bytes: TBytes;
S: String;
Begin
Result := '0';
S := sVal.Trim;
if (Length(S) = 0) then
Exit;
try
bytes := TBase58.BitCoin.Decode(S);
except Exit
end;
Result := TConverters.ConvertBytesToHexString(TBigInteger.Create(1, bytes).ToByteArrayUnsigned,False);
Result := TrimLeadingCeros(Result);
End;
Function ChecksumBase58(const S: String): integer;
var
C: Char;
Total: integer = 0;
Begin
for C in S do
Inc(Total, Pos(C, B58Alphabet)-1);
Result := Total;
End;
Function GetCertificateChecksum(certificate:String):String;
Begin
Result := BMB58resumenNew(Certificate);
Result := B10ToB58(Result);
if Length(Result) < 3 then AddChar('1',Result,3);
End;
// RETURN THE SUMATORY OF A BASE58
Function BMB58resumenNew(numero58:string):string;
var
counter, total : integer;
Begin
total := 0;
for counter := 1 to length(numero58) do
begin
total := total+Pos(numero58[counter],B58Alphabet)-1;
end;
Total := Total + length(numero58);
result := IntToStr(total);
End;
// RETURN THE SUMATORY OF A BASE58
Function BMB58resumen(numero58:string):string;
var
counter, total : integer;
Begin
total := 0;
for counter := 1 to length(numero58) do
begin
total := total+Pos(numero58[counter],B58Alphabet)-1;
end;
result := IntToStr(total);
End;
// RETURN THE SUMATORY OF A BASE58
Function BMB58resumenInt(numero58:string):integer;
var
counter, total : integer;
Begin
total := 0;
for counter := 1 to length(numero58) do
begin
total := total+Pos(numero58[counter],B58Alphabet)-1;
end;
result := total;
End;
{$REGION Big maths}
// *****************************************************************************
// ***************************FUNCTIONS OF BIGMATHS*****************************
// *****************************************************************************
// REMOVES LEFT CEROS
function ClearLeadingCeros(numero:string):string;
var
count : integer = 0;
movepos : integer = 0;
Begin
result := '';
if numero[1] = '-' then movepos := 1;
for count := 1+movepos to length(numero) do
begin
if numero[count] <> '0' then result := result + numero[count];
if ((numero[count]='0') and (length(result)>0)) then result := result + numero[count];
end;
if result = '' then result := '0';
if ((movepos=1) and (result <>'0')) then result := '-'+result;
End;
// ADDS 2 NUMBERS
function BMAdicion(numero1,numero2:string):string;
var
longitude : integer = 0;
count: integer = 0;
carry : integer = 0;
resultado : string = '';
thiscol : integer;
ceros : integer;
Begin
longitude := length(numero1);
if length(numero2)>longitude then
begin
longitude := length(numero2);
ceros := length(numero2)-length(numero1);
while count < ceros do
begin
numero1 := '0'+numero1;
count := count+1;
end;
end
else
begin
ceros := length(numero1)-length(numero2);
while count < ceros do
begin
numero2 := '0'+numero2;
count := count+1;
end;
end;
for count := longitude downto 1 do
Begin
thiscol := StrToInt(numero1[count]) + StrToInt(numero2[count])+carry;
carry := 0;
if thiscol > 9 then
begin
thiscol := thiscol-10;
carry := 1;
end;
resultado := inttoStr(thiscol)+resultado;
end;
if carry > 0 then resultado := '1'+resultado;
result := resultado;
End;
// DRAW CEROS FOR MULTIPLICATION
Function PonerCeros(numero:String;cuantos:integer):string;
var
contador : integer = 0;
NewNumber : string;
Begin
NewNumber := numero;
while contador < cuantos do
begin
NewNumber := NewNumber+'0';
contador := contador+1;
end;
result := NewNumber;
End;
// MULTIPLIER
Function BMMultiplicar(Numero1,Numero2:string):string;
var
count,count2 : integer;
sumandos : array of string;
thiscol : integer;
carry: integer = 0;
cantidaddeceros : integer = 0;
TotalSuma : string = '0';
Begin
setlength(sumandos,length(numero2));
for count := length(numero2) downto 1 do
begin
for count2 := length(numero1) downto 1 do
begin
thiscol := (StrToInt(numero2[count]) * StrToInt(numero1[count2])+carry);
carry := thiscol div 10;
ThisCol := ThisCol - (carry*10);
sumandos[cantidaddeceros] := IntToStr(thiscol)+ sumandos[cantidaddeceros];
end;
if carry > 0 then sumandos[cantidaddeceros] := IntToStr(carry)+sumandos[cantidaddeceros];
carry := 0;
sumandos[cantidaddeceros] := PonerCeros(sumandos[cantidaddeceros],cantidaddeceros);
cantidaddeceros := cantidaddeceros+1;
end;
for count := 0 to length(sumandos)-1 do
TotalSuma := BMAdicion(Sumandos[count],totalsuma);
result := TotalSuma;//ClearLeadingCeros(TotalSuma);
End;
// DIVIDES TWO NUMBERS
Function BMDividir(Numero1,Numero2:string):DivResult;
var
counter : integer;
cociente : string = '';
long : integer;
Divisor : Int64;
ThisStep : String = '';
Begin
long := length(numero1);
Divisor := StrToInt64(numero2);
for counter := 1 to long do
begin
ThisStep := ThisStep + Numero1[counter];
if StrToInt(ThisStep) >= Divisor then
begin
cociente := cociente+IntToStr(StrToInt(ThisStep) div Divisor);
ThisStep := (IntToStr(StrToInt(ThisStep) mod Divisor));
end
else cociente := cociente+'0';
end;
result.cociente := ClearLeadingCeros(cociente);
result.residuo := ClearLeadingCeros(thisstep);
End;
// CALCULATES A EXPONENTIAL NUMBER
Function BMExponente(Numero1,Numero2:string):string;
var
count : integer = 0;
resultado : string = '';
Begin
if numero2 = '1' then result := numero1
else if numero2 = '0' then result := '1'
else
begin
resultado := numero1;
for count := 2 to StrToInt(numero2) do
resultado := BMMultiplicar(resultado,numero1);
result := resultado;
end;
End;
// HEX TO DECIMAL
function BMHexToDec(numerohex:string):string;
var
DecValues : array of integer;
ExpValues : array of string;
MultipliValues : array of string;
counter : integer;
Long : integer;
Resultado : string = '0';
Begin
Long := length(numerohex);
numerohex := uppercase(numerohex);
setlength(DecValues,0);
setlength(ExpValues,0);
setlength(MultipliValues,0);
setlength(DecValues,Long);
setlength(ExpValues,Long);
setlength(MultipliValues,Long);
for counter := 1 to Long do
DecValues[counter-1] := Pos(NumeroHex[counter],HexAlphabet)-1;
for counter := 1 to long do
ExpValues[counter-1] := BMExponente('16',IntToStr(long-counter));
for counter := 1 to Long do
MultipliValues[counter-1] := BMMultiplicar(ExpValues[counter-1],IntToStr(DecValues[counter-1]));
for counter := 1 to long do
Resultado := BMAdicion(resultado,MultipliValues[counter-1]);
result := resultado;
End;
Function BM58ToDec(number58:string):String;
var
long,counter : integer;
Resultado : string = '0';
DecValues : array of integer;
ExpValues : array of string;
MultipliValues : array of string;
Begin
Long := length(number58);
setlength(DecValues,0);
setlength(ExpValues,0);
setlength(MultipliValues,0);
setlength(DecValues,Long);
setlength(ExpValues,Long);
setlength(MultipliValues,Long);
for counter := 1 to Long do
DecValues[counter-1] := Pos(number58[counter],B58Alphabet)-1;
for counter := 1 to long do
ExpValues[counter-1] := BMExponente('58',IntToStr(long-counter));
for counter := 1 to Long do
MultipliValues[counter-1] := BMMultiplicar(ExpValues[counter-1],IntToStr(DecValues[counter-1]));
for counter := 1 to long do
Resultado := BMAdicion(resultado,MultipliValues[counter-1]);
result := resultado;
End;
// Hex to base 58
function BMHexTo58(numerohex:string;alphabetnumber:integer):string;
var
decimalvalue : string;
restante : integer;
ResultadoDiv : DivResult;
Resultado : string = '';
AlpahbetUsed : String;
Begin
AlpahbetUsed := B58Alphabet;
if alphabetnumber=36 then AlpahbetUsed := B36Alphabet;
decimalvalue := BMHexToDec(numerohex);
while length(decimalvalue) >= 2 do
begin
ResultadoDiv := BMDividir(decimalvalue,IntToStr(alphabetnumber));
DecimalValue := Resultadodiv.cociente;
restante := StrToInt(ResultadoDiv.residuo);
resultado := AlpahbetUsed[restante+1]+resultado;
end;
if StrToInt(decimalValue) >= alphabetnumber then
begin
ResultadoDiv := BMDividir(decimalvalue,IntToStr(alphabetnumber));
DecimalValue := Resultadodiv.cociente;
restante := StrToInt(ResultadoDiv.residuo);
resultado := AlpahbetUsed[restante+1]+resultado;
end;
if StrToInt(decimalvalue) > 0 then resultado := AlpahbetUsed[StrToInt(decimalvalue)+1]+resultado;
result := resultado;
End;
// CONVERTS A DECIMAL VALUE TO A BASE58 STRING
function BMDecTo58(numero:string):string;
var
decimalvalue : string;
restante : integer;
ResultadoDiv : DivResult;
Resultado : string = '';
Begin
decimalvalue := numero;
while length(decimalvalue) >= 2 do
begin
ResultadoDiv := BMDividir(decimalvalue,'58');
DecimalValue := Resultadodiv.cociente;
restante := StrToInt(ResultadoDiv.residuo);
resultado := B58Alphabet[restante+1]+resultado;
end;
if StrToInt(decimalValue) >= 58 then
begin
ResultadoDiv := BMDividir(decimalvalue,'58');
DecimalValue := Resultadodiv.cociente;
restante := StrToInt(ResultadoDiv.residuo);
resultado := B58Alphabet[restante+1]+resultado;
end;
if StrToInt(decimalvalue) > 0 then resultado := B58Alphabet[StrToInt(decimalvalue)+1]+resultado;
result := resultado;
End;
// CONVERTS A DECIMAL VALUE TO A HEX STRING
function BMDecToHex(numero:string):string;
var
decimalvalue : string;
restante : integer;
ResultadoDiv : DivResult;
Resultado : string = '';
Begin
decimalvalue := numero;
while length(decimalvalue) >= 2 do
begin
ResultadoDiv := BMDividir(decimalvalue,'16');
DecimalValue := Resultadodiv.cociente;
restante := StrToInt(ResultadoDiv.residuo);
resultado := HexAlphabet[restante+1]+resultado;
end;
if StrToInt(decimalValue) >= 16 then
begin
ResultadoDiv := BMDividir(decimalvalue,'16');
DecimalValue := Resultadodiv.cociente;
restante := StrToInt(ResultadoDiv.residuo);
resultado := HexAlphabet[restante+1]+resultado;
end;
if StrToInt(decimalvalue) > 0 then resultado := HexAlphabet[StrToInt(decimalvalue)+1]+resultado;
result := resultado;
End;
{$ENDREGION BigMaths}
END.{Unit}