-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhashgen.go
More file actions
2947 lines (2641 loc) · 73.5 KB
/
Copy pathhashgen.go
File metadata and controls
2947 lines (2641 loc) · 73.5 KB
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
package main
import (
"bufio"
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base32"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"hash/crc32"
"hash/crc64"
"io"
"log"
"math/bits"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
"unicode/utf16"
"unicode/utf8"
"github.com/cyclone-github/base58"
"github.com/cyclone-github/md6"
"github.com/ebfe/keccak" // keccak 224/384
"github.com/emmansun/gmsm/sm3"
"github.com/openwall/yescrypt-go"
"github.com/tarantool/go-gostcrypto/streebog"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/scrypt"
"golang.org/x/crypto/sha3"
)
/*
hashgen is a CLI hash generator which can be cross-compiled for Linux, Raspberry Pi, Windows & Mac
https://github.com/cyclone-github/hashgen
written by cyclone
GNU General Public License v2.0
https://github.com/cyclone-github/hashgen/blob/main/LICENSE
full changelog
https://github.com/cyclone-github/hashgen/blob/main/CHANGELOG.md
latest changelog
v1.3.0; 2026-04-11
added scrypt mode: -m 8900
added PBKDF2 modes: -m 10900, 11900, 12000, 12100
added BLAKE2 modes: -m 600, 610, 620, 31000, 34800, 34810, 34820, 33300
added HMAC modes: -m 50, 60, 150, 160, 1450, 1460, 1750, 1760, 6050, 6060
added UTF-16LE modes: -m 30, 40, 70, 130, 140, 170, 1430, 1440, 1470, 1730, 1740, 1770
optimized salt RNG for fewer syscalls on salted hash modes
added sanity check on invalid -m mode before opening stdin/wordlist
compiled with Go v1.26.2
v1.3.1; 2026-04-13
add modes: MD6-128, MD6-224, MD6-256, MD6-384, MD6-512
v1.3.2; 2026-08-18
add mode: gost-yescrypt
add mode: SSHA -m 111
add mode: sha1crypt -m 15100
add mode: sm3crypt -m 35100
add mode: cmiyc (KoreLogic CMIYC 2026 contest algorithm)
add modes: Streebog/GOST 2012 -m 11700, 11750, 11760, 11800, 11850, 11860
add modes: SHA-384 UTF-16LE -m 10830, 10840, 10870
add modes: LDAP SHA/SSHA -m 101, 1411, 1711
add modes: -m 3500, 4300, 4400, 4700, 18500, 18501, 20800, 32800, 34400, 34500, 35900
refactor modes: -m 2600 and 4500
add bcrypt prehash modes: -m 25600, 25800, 28400, 30600
add hashcat alias: wpbcrypt -m 35500
*/
func versionFunc() {
fmt.Fprintln(os.Stderr, "hashgen v1.3.2; 2026-08-18\nhttps://github.com/cyclone-github/hashgen")
}
// help function
func helpFunc() {
versionFunc()
str := "\nExample Usage:\n" +
"\n./hashgen -m md5 -w wordlist.txt -o output.txt\n" +
"./hashgen -m bcrypt -cost 8 -w wordlist.txt\n" +
"./hashgen -m 10900 -i 2000 -w wordlist.txt\n" +
"cat wordlist | ./hashgen -m md5 -hashplain\n" +
"\nAll Supported Options:\n-m {mode}\n-w {wordlist input}\n-t {cpu threads}\n-o {wordlist output}\n-b {benchmark mode}\n-cost {bcrypt only; default 10}\n-i {PBKDF2 iterations for 10900-12100; 0 means 1000; invalid with other modes}\n-hashplain {hash:plain output}\n-help {this text}\n-version {version string}\n" +
"\nIf -w is not specified, defaults to stdin\n" +
"If -o is not specified, defaults to stdout\n" +
"If -t is not specified, defaults to max available CPU threads\n" +
"\nModes:\t\t\tHashcat Mode (notes):\n" +
"plaintext/dehex 99999 (decode $HEX[])\n" +
"hex (encode to $HEX[])\n" +
"base32decode\n" +
"base32encode\n" +
"base58decode\n" +
"base58encode\n" +
"base64decode\n" +
"base64encode\n" +
"morsecode (ITU-R M.1677-1)\n" +
"morsedecode (ITU-R M.1677-1)\n" +
"\n" +
"crc32\n" +
"11500 (CRC32 Hashcat format)\n" +
"crc64\n" +
"\n" +
"md4 900\n" +
"md5 0\n" +
"halfmd5 5100\n" +
"md5passsalt 10\n" +
"md5saltpass 20\n" +
"md5utf16passsalt 30\n" +
"md5utf16saltpass 40\n" +
"hmacmd5pass 50\n" +
"hmacmd5salt 60\n" +
"md5utf16le 70\n" +
"md5md5 2600\n" +
"3500 (md5(md5(md5($pass))))\n" +
"4300 (md5(strtoupper(md5($pass))))\n" +
"4400 (md5(sha1($pass)))\n" +
"32800 (md5(sha1(md5($pass))))\n" +
"\n" +
"md6128\n" +
"md6224\n" +
"md6256 34600\n" +
"md6384\n" +
"md6512\n" +
"\n" +
"sha1 100\n" +
"ldapsha 101 (Netscape LDAP {SHA})\n" +
"sha1passsalt 110\n" +
"ssha 111 (NSLDAPS SSHA-1)\n" +
"sha1saltpass 120\n" +
"sha1utf16passsalt 130\n" +
"sha1utf16saltpass 140\n" +
"hmacsha1pass 150\n" +
"hmacsha1salt 160\n" +
"sha1utf16le 170\n" +
"sha1sha1 4500\n" +
"4700 (sha1(md5($pass)))\n" +
"18500 (sha1(md5(md5($pass))))\n" +
"18501 (sha1(md5(sha1($pass))))\n" +
"\n" +
"sha224 1300\n" +
"sha224passsalt 1310\n" +
"sha224saltpass 1320\n" +
"34400 (sha224(sha224($pass)))\n" +
"34500 (sha224(sha1($pass)))\n" +
"sha256 1400\n" +
"sha256passsalt 1410\n" +
"ssha256 1411 (LDAP {SSHA256})\n" +
"sha256saltpass 1420\n" +
"sha256utf16passsalt 1430\n" +
"sha256utf16saltpass 1440\n" +
"hmacsha256pass 1450\n" +
"hmacsha256salt 1460\n" +
"sha256utf16le 1470\n" +
"20800 (sha256(md5($pass)))\n" +
"35900 (sha256(sha1($pass)))\n" +
"sha384 10800\n" +
"sha384passsalt 10810\n" +
"sha384saltpass 10820\n" +
"sha384utf16passsalt 10830\n" +
"sha384utf16saltpass 10840\n" +
"sha384utf16le 10870\n" +
"sha512 1700\n" +
"sha512passsalt 1710\n" +
"ssha512 1711 (LDAP {SSHA512})\n" +
"sha512saltpass 1720\n" +
"sha512utf16passsalt 1730\n" +
"sha512utf16saltpass 1740\n" +
"hmacsha512pass 1750\n" +
"hmacsha512salt 1760\n" +
"sha512utf16le 1770\n" +
"sha512224\n" +
"sha512256\n" +
"\n" +
"sha3224 17300\n" +
"sha3256 17400\n" +
"sha3384 17500\n" +
"sha3512 17600\n" +
"\n" +
"keccak224 17700\n" +
"keccak256 17800\n" +
"keccak384 17900\n" +
"keccak512 18000\n" +
"\n" +
"blake2s256\n" +
"31000 (BLAKE2s-256 $BLAKE2$ format)\n" +
"hmacblake2spass 33300\n" +
"blake2b256\n" +
"34800 (BLAKE2b-256 $BLAKE2$ format)\n" +
"blake2b256passsalt 34810\n" +
"blake2b256saltpass 34820\n" +
"blake2b384\n" +
"blake2b512\n" +
"600 (BLAKE2b-512 $BLAKE2$ format)\n" +
"blake2b512passsalt 610\n" +
"blake2b512saltpass 620\n" +
"\n" +
"ntlm 1000 (Windows NT)\n" +
"mysql4/mysql5 300\n" +
"ripemd160 6000\n" +
"hmacripemd160pass 6050\n" +
"hmacripemd160salt 6060\n" +
"\n" +
"streebog256 11700\n" +
"hmacstreebog256pass 11750\n" +
"hmacstreebog256salt 11760\n" +
"streebog512 11800\n" +
"hmacstreebog512pass 11850\n" +
"hmacstreebog512salt 11860\n" +
"\n" +
"argon2id 34000\n" +
"bcrypt 3200\n" +
"bcryptmd5 25600\n" +
"bcryptsha1 25800\n" +
"bcryptsha512 28400\n" +
"bcryptsha256 30600\n" +
"wpbcrypt 35500 (WordPress bcrypt-HMAC-SHA384)\n" +
"md5crypt 500 (Linux shadow $1$)\n" +
"sha1crypt 15100 (NetBSD/Juniper SHA1 crypt)\n" +
"sha256crypt 7400 (Linux shadow $5$)\n" +
"sha512crypt 1800 (Linux shadow $6$)\n" +
"sm3crypt 35100 (Unix $sm3$)\n" +
"phpass/phpbb3 400 ($P$ / $H$)\n" +
"scrypt 8900\n" +
"pbkdf2sha256 10900\n" +
"pbkdf2md5 11900\n" +
"pbkdf2sha1 12000\n" +
"pbkdf2sha512 12100\n" +
"yescrypt (Linux shadow $y$)\n" +
"gostyescrypt (Linux shadow $gy$)\n" +
"cmiyc (KoreLogic CMIYC 2026 contest algorithm)"
fmt.Fprintln(os.Stderr, str)
os.Exit(0)
}
var (
cryptBase64 = []byte("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
)
// pbkdf2 iteration count from -i for 10900-12100, default 1000
var pbkdf2IterCount = 1000
// set during -m probe so heavy modes return without hashing
var modeProbe bool
func isPBKDF2Mode(m string) bool {
switch m {
case "10900", "pbkdf2sha256", "pbkdf2-sha256", "11900", "pbkdf2md5", "pbkdf2-md5", "12000", "pbkdf2sha1", "pbkdf2-sha1", "12100", "pbkdf2sha512", "pbkdf2-sha512":
return true
default:
return false
}
}
// pooled scratch to batch crypto/rand reads
const cryptoRandChunk = 256
type cryptoRandBuf struct {
b [cryptoRandChunk]byte
off int
}
// pool buffer starts drained so first read is real random not zeros
var cryptoRandPool = sync.Pool{
New: func() any { return &cryptoRandBuf{off: cryptoRandChunk} },
}
// fill p from pooled crypto/rand, one buffer per call
func readRand(p []byte) bool {
if len(p) == 0 {
return true
}
buf := cryptoRandPool.Get().(*cryptoRandBuf)
defer cryptoRandPool.Put(buf)
for len(p) > 0 {
if buf.off >= len(buf.b) {
if _, err := rand.Read(buf.b[:]); err != nil {
fmt.Fprintln(os.Stderr, "random read error:", err)
return false
}
buf.off = 0
}
n := copy(p, buf.b[buf.off:])
buf.off += n
p = p[n:]
}
return true
}
// 8 random bytes as 16 hex chars into dst
func fillSaltHex8(dst []byte) bool {
if len(dst) < 16 {
return false
}
var raw [8]byte
if !readRand(raw[:]) {
return false
}
hex.Encode(dst, raw[:])
return true
}
// utf-16le from utf-8 password, same validity rules as ntlm
func utf16LEPassStrict(data []byte) ([]byte, bool) {
var rs []rune
for i := 0; i < len(data); {
r, sz := utf8.DecodeRune(data[i:])
if r == utf8.RuneError && sz == 1 {
return nil, false
}
if r >= 0xD800 && r <= 0xDFFF {
return nil, false
}
rs = append(rs, r)
i += sz
}
u16 := utf16.Encode(rs)
out := make([]byte, 2*len(u16))
for i, c := range u16 {
out[2*i] = byte(c)
out[2*i+1] = byte(c >> 8)
}
return out, true
}
// hash composition helpers return lowercase hex text for the next stage
func md5HexBytes(data []byte) [32]byte {
sum := md5.Sum(data)
var out [32]byte
hex.Encode(out[:], sum[:])
return out
}
func sha1HexBytes(data []byte) [40]byte {
sum := sha1.Sum(data)
var out [40]byte
hex.Encode(out[:], sum[:])
return out
}
func sha224HexBytes(data []byte) [56]byte {
sum := sha256.Sum224(data)
var out [56]byte
hex.Encode(out[:], sum[:])
return out
}
func sha256HexBytes(data []byte) [64]byte {
sum := sha256.Sum256(data)
var out [64]byte
hex.Encode(out[:], sum[:])
return out
}
func sha512HexBytes(data []byte) [128]byte {
sum := sha512.Sum512(data)
var out [128]byte
hex.Encode(out[:], sum[:])
return out
}
// LDAP SHA/SSHA family -m 101 / 111 / 1411 / 1711
func ldapSHA(password []byte, mode string, saltRaw []byte) string {
if mode == "101" || mode == "ldapsha" || mode == "ldap-sha" {
digest := sha1.Sum(password)
return "{SHA}" + base64.StdEncoding.EncodeToString(digest[:])
}
salt := saltRaw
if salt == nil {
salt = make([]byte, 8)
if !readRand(salt) {
return ""
}
}
var prefix string
var digest []byte
switch mode {
case "111", "ssha":
prefix = "{SSHA}"
h := sha1.New()
_, _ = h.Write(password)
_, _ = h.Write(salt)
digest = h.Sum(nil)
case "1411", "ssha256":
prefix = "{SSHA256}"
h := sha256.New()
_, _ = h.Write(password)
_, _ = h.Write(salt)
digest = h.Sum(nil)
case "1711", "ssha512":
prefix = "{SSHA512}"
h := sha512.New()
_, _ = h.Write(password)
_, _ = h.Write(salt)
digest = h.Sum(nil)
default:
return ""
}
payload := make([]byte, 0, len(digest)+len(salt))
payload = append(payload, digest...)
payload = append(payload, salt...)
return prefix + base64.StdEncoding.EncodeToString(payload)
}
// sha384 UTF-16LE family -m 10830 / 10840 / 10870
func sha384UTF16(password []byte, mode string, saltRaw []byte) string {
pwU16, ok := utf16LEPassStrict(password)
if !ok {
return ""
}
if mode == "10870" || mode == "sha384utf16le" {
h := sha512.New384()
h.Write(pwU16)
return hex.EncodeToString(h.Sum(nil))
}
salt := saltRaw
if salt == nil {
salt = make([]byte, 16)
if !fillSaltHex8(salt) {
return ""
}
}
h := sha512.New384()
if mode == "10840" || mode == "sha384utf16saltpass" {
h.Write(salt)
h.Write(pwU16)
} else {
h.Write(pwU16)
h.Write(salt)
}
sum := h.Sum(nil)
out := make([]byte, 96+1+len(salt))
hex.Encode(out[:96], sum)
out[96] = ':'
copy(out[97:], salt)
return string(out)
}
// hashcat hmac line as hex(digest):salt
func hexMACLineDigest(digest []byte, salt []byte) string {
n := hex.EncodedLen(len(digest))
out := make([]byte, n+1+len(salt))
hex.Encode(out[:n], digest)
out[n] = ':'
copy(out[n+1:], salt)
return string(out)
}
// reverse digest byte order for hashcat big-endian Streebog modes
func reverseBytes(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}
// hashcat blake2s_hmac for mode 33300
func blake2sHMACHashcat(key, msg []byte) []byte {
const block = 64
var kpad [block]byte
if len(key) > block {
s := blake2s.Sum256(key)
copy(kpad[:], s[:])
} else {
copy(kpad[:], key)
}
var ipadBlock, opadBlock [block]byte
for i := 0; i < block; i++ {
ipadBlock[i] = kpad[i] ^ 0x36
opadBlock[i] = kpad[i] ^ 0x5c
}
ih, _ := blake2s.New256(nil)
_, _ = ih.Write(ipadBlock[:])
_, _ = ih.Write(msg)
inner := ih.Sum(nil)
oh, _ := blake2s.New256(nil)
_, _ = oh.Write(opadBlock[:])
_, _ = oh.Write(inner)
return oh.Sum(nil)
}
// dehex wordlist line
/* note:
the checkForHex() function below gives a best effort in decoding all HEX strings and applies error correction when needed
if your wordlist contains HEX strings that resemble alphabet soup, don't be surprised if you find "garbage in" still means "garbage out"
the best way to fix HEX decoding issues is to correctly parse your wordlists so you don't end up with foobar HEX strings
if you have suggestions on how to better handle HEX decoding errors, contact me on github
*/
func checkForHex(line []byte) ([]byte, []byte, int) {
// check if line is in $HEX[] format
const prefix = "$HEX["
if len(line) >= len(prefix) && bytes.HasPrefix(line, []byte(prefix)) {
// attempt to correct improperly formatted $HEX[] entries
// if it doesn't end with "]", add the missing bracket
var hexErrorDetected int
hasClose := bytes.HasSuffix(line, []byte("]"))
if !hasClose {
hexErrorDetected = 1 // mark as error since the format was corrected
}
// find first '[' and last ']'
startIdx := bytes.IndexByte(line, '[')
endIdx := bytes.LastIndexByte(line, ']')
if endIdx == -1 {
endIdx = len(line) // pretend ']' is at end
}
hexContent := line[startIdx+1 : endIdx]
// decode hex content into bytes
var decodedBytes []byte
if n := len(hexContent); n > 0 && (n&1) == 0 {
decodedBytes = make([]byte, n/2)
if _, err := hex.Decode(decodedBytes, hexContent); err == nil {
disp := make([]byte, 5+len(hexContent)+1) // "$HEX[" + hex + "]"
copy(disp, prefix)
copy(disp[5:], hexContent)
disp[len(disp)-1] = ']'
return decodedBytes, disp, hexErrorDetected
}
hexErrorDetected = 1
} else {
hexErrorDetected = 1
}
// error handling: remove invalid hex chars
clean := make([]byte, 0, len(hexContent))
for _, c := range hexContent {
lc := c | 0x20
if (c >= '0' && c <= '9') || (lc >= 'a' && lc <= 'f') {
clean = append(clean, c)
}
}
// if hex has an odd length, add a zero nibble to make it even
if len(clean)%2 != 0 {
clean = append([]byte{'0'}, clean...)
}
decodedBytes = make([]byte, len(clean)/2)
if len(clean) == 0 || func() bool {
_, err := hex.Decode(decodedBytes, clean)
return err != nil
}() {
log.Printf("Error decoding $HEX[] content")
// if decoding still fails, return original line as bytes
disp := make([]byte, 5+len(hexContent)+1)
copy(disp, prefix)
copy(disp[5:], hexContent)
disp[len(disp)-1] = ']'
return line, disp, hexErrorDetected
}
// return decoded bytes and formatted hex content
disp := make([]byte, 5+len(hexContent)+1)
copy(disp, prefix)
copy(disp[5:], hexContent)
disp[len(disp)-1] = ']'
return decodedBytes, disp, hexErrorDetected
}
// return original line as bytes if not in $HEX[] format
return line, line, 0
}
// ITU-R M.1677-1 standard morse code mapping
// https://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1677-1-200910-I!!PDF-E.pdf
// both upper and lowercase alpha were included due to using byte arrays for speed optimization
var morseCodeMap = map[byte]string{
// lowercase alpha
'a': ".-", 'b': "-...", 'c': "-.-.", 'd': "-..", 'e': ".",
'f': "..-.", 'g': "--.", 'h': "....", 'i': "..", 'j': ".---",
'k': "-.-", 'l': ".-..", 'm': "--", 'n': "-.", 'o': "---",
'p': ".--.", 'q': "--.-", 'r': ".-.", 's': "...", 't': "-",
'u': "..-", 'v': "...-", 'w': ".--", 'x': "-..-", 'y': "-.--",
'z': "--..",
// uppercase alpha
'A': ".-", 'B': "-...", 'C': "-.-.", 'D': "-..", 'E': ".",
'F': "..-.", 'G': "--.", 'H': "....", 'I': "..", 'J': ".---",
'K': "-.-", 'L': ".-..", 'M': "--", 'N': "-.", 'O': "---",
'P': ".--.", 'Q': "--.-", 'R': ".-.", 'S': "...", 'T': "-",
'U': "..-", 'V': "...-", 'W': ".--", 'X': "-..-", 'Y': "-.--",
'Z': "--..",
// digits
'0': "-----", '1': ".----", '2': "..---", '3': "...--", '4': "....-",
'5': ".....", '6': "-....", '7': "--...", '8': "---..", '9': "----.",
// special char
'.': ".-.-.-", ',': "--..--", '?': "..--..", '\'': ".----.", '!': "-.-.--",
'/': "-..-.", '(': "-.--.", ')': "-.--.-", '&': ".-...", ':': "---...",
';': "-.-.-.", '=': "-...-", '+': ".-.-.", '-': "-....-", '_': "..--.-",
'"': ".-..-.", '$': "...-..-", '@': ".--.-.", ' ': " ",
// procedural signs were intentionally omitted
}
// encode byte slice to Morse Code
func encodeToMorseBytes(input []byte) []byte {
var encoded bytes.Buffer
for _, char := range input {
if code, exists := morseCodeMap[char]; exists {
encoded.WriteString(code)
encoded.WriteByte(' ') // add space after each Morse Code sequence
}
}
// remove trailing space
result := encoded.Bytes()
if len(result) > 0 && result[len(result)-1] == ' ' {
return result[:len(result)-1]
}
return result
}
/*
decode Morse Code to bytes
rules:
- single space between codes -> next character
- 3 or more spaces -> word separator (space - matches -m morsecode encoder style)
*/
func decodeMorseBytes(input []byte) []byte {
var out bytes.Buffer
var token bytes.Buffer
spaces := 0
flushToken := func() {
if token.Len() == 0 {
return
}
code := token.String()
if ch, ok := morseDecodeMap[code]; ok {
out.WriteByte(ch)
}
token.Reset()
}
for _, b := range input {
if b == ' ' || b == '\t' {
spaces++
continue
}
// non-space
if spaces > 0 {
flushToken()
if spaces >= 3 {
out.WriteByte(' ') // word separator
}
spaces = 0
}
token.WriteByte(b)
}
flushToken()
return out.Bytes()
}
// reverse lookup for Morse Code -> byte (prefer uppercase letters)
var morseDecodeMap = func() map[string]byte {
m := make(map[string]byte)
for ch, code := range morseCodeMap {
if code == " " {
continue
}
// store as uppercase
up := ch
if up >= 'a' && up <= 'z' {
up -= 32
}
m[code] = up
}
return m
}()
// phpassMD5 -m 400
func phpassMD5(password []byte, mode string, countLog2 int, saltRaw []byte) string {
prefix := byte('P')
if mode == "phpbb3" {
prefix = 'H'
}
if countLog2 <= 0 {
countLog2 = 11
}
if len(saltRaw) < 6 {
saltRaw = make([]byte, 6)
if !readRand(saltRaw) {
return ""
}
} else if len(saltRaw) > 6 {
saltRaw = saltRaw[:6]
}
encode64 := func(src []byte, outLen int) []byte {
dst := make([]byte, 0, outLen)
for i := 0; i < len(src); {
var v uint32 = uint32(src[i])
i++
dst = append(dst, cryptBase64[v&0x3f])
if i < len(src) {
v |= uint32(src[i]) << 8
}
dst = append(dst, cryptBase64[(v>>6)&0x3f])
if i >= len(src) {
break
}
i++
if i < len(src) {
v |= uint32(src[i]) << 16
}
dst = append(dst, cryptBase64[(v>>12)&0x3f])
if i >= len(src) {
break
}
i++
dst = append(dst, cryptBase64[(v>>18)&0x3f])
if len(dst) >= outLen {
break
}
}
if len(dst) > outLen {
return dst[:outLen]
}
return dst
}
saltEnc := encode64(saltRaw, 8)
h := md5.New()
h.Write(saltEnc)
h.Write(password)
sum := h.Sum(nil)
rounds := 1 << countLog2
for i := 0; i < rounds; i++ {
h.Reset()
h.Write(sum)
h.Write(password)
sum = h.Sum(nil)
}
digestEnc := encode64(sum, 22)
out := make([]byte, 3+1+8+22)
out[0], out[1], out[2] = '$', prefix, '$'
out[3] = cryptBase64[countLog2]
copy(out[4:12], saltEnc)
copy(out[12:], digestEnc)
return string(out)
}
// md5crypt - Linux shadow "$1$<salt>$<hash>"
func md5crypt(password []byte) string {
const magic = "$1$"
salt := make([]byte, 8)
rb := make([]byte, 8)
if !readRand(rb) {
for i := 0; i < 8; i++ {
salt[i] = cryptBase64[(i*37+13)&0x3f]
}
} else {
for i := 0; i < 8; i++ {
salt[i] = cryptBase64[int(rb[i])&0x3f]
}
}
a := md5.New()
a.Write(password)
a.Write([]byte(magic))
a.Write(salt)
alt := md5.New()
alt.Write(password)
alt.Write(salt)
alt.Write(password)
altSum := alt.Sum(nil)
pwLen := len(password)
for n := pwLen; n > 0; n -= 16 {
if n > 16 {
a.Write(altSum)
} else {
a.Write(altSum[:n])
}
}
for n := pwLen; n > 0; n >>= 1 {
if (n & 1) == 1 {
a.Write([]byte{0})
} else if pwLen > 0 {
a.Write(password[:1])
} else {
a.Write([]byte{0})
}
}
final := a.Sum(nil)
for i := 0; i < 1000; i++ {
ri := md5.New()
if (i & 1) == 1 {
ri.Write(password)
} else {
ri.Write(final)
}
if i%3 != 0 {
ri.Write(salt)
}
if i%7 != 0 {
ri.Write(password)
}
if (i & 1) == 1 {
ri.Write(final)
} else {
ri.Write(password)
}
final = ri.Sum(nil)
}
out := make([]byte, 0, 3+1+8+1+22)
out = append(out, magic...)
out = append(out, salt...)
out = append(out, '$')
var v uint32
v = uint32(final[0])<<16 | uint32(final[6])<<8 | uint32(final[12])
for j := 0; j < 4; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
v = uint32(final[1])<<16 | uint32(final[7])<<8 | uint32(final[13])
for j := 0; j < 4; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
v = uint32(final[2])<<16 | uint32(final[8])<<8 | uint32(final[14])
for j := 0; j < 4; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
v = uint32(final[3])<<16 | uint32(final[9])<<8 | uint32(final[15])
for j := 0; j < 4; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
v = uint32(final[4])<<16 | uint32(final[10])<<8 | uint32(final[5])
for j := 0; j < 4; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
v = uint32(0)<<16 | uint32(0)<<8 | uint32(final[11])
for j := 0; j < 2; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
return string(out)
}
// sha256crypt - Linux shadow $5$[rounds=R$]<salt>$<hash>
func sha256crypt(password []byte) string {
const magic = "$5$"
rounds := 5000
salt := make([]byte, 16)
rb := make([]byte, 16)
if !readRand(rb) {
for i := 0; i < 16; i++ {
salt[i] = cryptBase64[(i*37+13)&0x3f]
}
} else {
for i := 0; i < 16; i++ {
salt[i] = cryptBase64[int(rb[i])&0x3f]
}
}
a := sha256.New()
a.Write(password)
a.Write(salt)
alt := sha256.New()
alt.Write(password)
alt.Write(salt)
alt.Write(password)
altSum := alt.Sum(nil)
pwLen := len(password)
for n := pwLen; n > 0; n -= 32 {
if n > 32 {
a.Write(altSum)
} else {
a.Write(altSum[:n])
}
}
for n := pwLen; n > 0; n >>= 1 {
if (n & 1) == 1 {
a.Write(altSum)
} else {
a.Write(password)
}
}
adigest := a.Sum(nil)
dp := sha256.New()
for i := 0; i < pwLen; i++ {
dp.Write(password)
}
dpSum := dp.Sum(nil)
P := make([]byte, pwLen)
for i := 0; i < pwLen; i += 32 {
end := i + 32
if end > pwLen {
end = pwLen
}
copy(P[i:end], dpSum[:end-i])
}
ds := sha256.New()
for i := 0; i < 16+int(adigest[0]); i++ {
ds.Write(salt)
}
dsSum := ds.Sum(nil)
S := make([]byte, len(salt))
copy(S, dsSum[:len(salt)])
digest := adigest
for i := 0; i < rounds; i++ {
c := sha256.New()
if (i & 1) == 1 {
c.Write(P)
} else {
c.Write(digest)
}
if i%3 != 0 {
c.Write(S)
}
if i%7 != 0 {
c.Write(P)
}
if (i & 1) == 1 {
c.Write(digest)
} else {
c.Write(P)
}
digest = c.Sum(nil)
}
out := make([]byte, 0, len(magic)+len(salt)+1+43)
out = append(out, magic...)
out = append(out, salt...)
out = append(out, '$')
enc := func(b2, b1, b0 byte, n int) {
v := uint32(b2)<<16 | uint32(b1)<<8 | uint32(b0)
for j := 0; j < n; j++ {
out = append(out, cryptBase64[v&0x3f])
v >>= 6
}
}
enc(digest[0], digest[10], digest[20], 4)
enc(digest[21], digest[1], digest[11], 4)
enc(digest[12], digest[22], digest[2], 4)
enc(digest[3], digest[13], digest[23], 4)
enc(digest[24], digest[4], digest[14], 4)
enc(digest[15], digest[25], digest[5], 4)
enc(digest[6], digest[16], digest[26], 4)
enc(digest[27], digest[7], digest[17], 4)
enc(digest[18], digest[28], digest[8], 4)
enc(digest[9], digest[19], digest[29], 4)
enc(0, digest[31], digest[30], 3)
return string(out)
}
// sha512crypt - Linux shadow ($6$)
func sha512crypt(password []byte) string {
const magic = "$6$"
const rounds = 5000
salt := make([]byte, 16)
{
var rb [16]byte
if !readRand(rb[:]) {
return ""