-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhashgen.go
More file actions
2266 lines (2043 loc) · 57.6 KB
/
hashgen.go
File metadata and controls
2266 lines (2043 loc) · 57.6 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/openwall/yescrypt-go"
"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 nth before opening stdin/wordlist
compiled with Go v1.26.2
v1.3.1; 2026-04-13
add modes: MD6-128, 224, 256, 384, 512
*/
func versionFunc() {
fmt.Fprintln(os.Stderr, "hashgen v1.3.1; 2026-04-13\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 / wpbcrypt 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\tHashcat Mode (notes):\n" +
"argon2id\t34000\n" +
"base32decode\n" +
"base32encode\n" +
"base58decode\n" +
"base58encode\n" +
"base64decode\n" +
"base64encode\n" +
"bcrypt\t\t3200\n" +
"blake2s-256\n" +
"31000\t\t(hashcat compatible BLAKE2s-256)\n" +
"33300\t\t(hashcat compatible HMAC-BLAKE2s key = $pass)\n" +
"blake2b-256\n" +
"34800\t\t(hashcat compatible BLAKE2b-256)\n" +
"blake2b-384\n" +
"blake2b-512\n" +
"600\t\t(hashcat compatible BLAKE2b-512)\n" +
"610\t\t(hashcat compatible BLAKE2b-512 pass.salt)\n" +
"620\t\t(hashcat compatible BLAKE2b-512 salt.pass)\n" +
"34810\t\t(hashcat compatible BLAKE2b-256 pass.salt)\n" +
"34820\t\t(hashcat compatible BLAKE2b-256 salt.pass)\n" +
"crc32\n" +
"11500\t\t(hashcat compatible CRC32)\n" +
"crc64\n" +
"hex\t\t(encode to $HEX[])\n" +
"dehex/plaintext\t99999 (decode $HEX[])\n" +
"md4\t\t900\n" +
"md5\t\t0\n" +
"halfmd5\t\t5100\n" +
"md5passsalt\t10\n" +
"md5saltpass\t20\n" +
"30\t\t(hashcat compatible md5 utf16le($pass).$salt)\n" +
"40\t\t(hashcat compatible md5 $salt.utf16le($pass))\n" +
"70\t\t(hashcat compatible md5 utf16le($pass))\n" +
"md5md5\t\t2600\n" +
"md5crypt\t500 (Linux shadow $1$)\n" +
"md6-128\n" +
"md6-224\n" +
"md6-256\t\t34600\n" +
"md6-384\n" +
"md6-512\n" +
"mysql4/mysql5\t300\n" +
"morsecode\t(ITU-R M.1677-1)\n" +
"morsedecode\t(ITU-R M.1677-1)\n" +
"ntlm\t\t1000 (Windows NT)\n" +
"phpass\t\t400\n" +
"ripemd-160\t6000\n" +
"sha1\t\t100\n" +
"sha1sha1\t4500\n" +
"sha1passsalt\t110\n" +
"sha1saltpass\t120\n" +
"130\t\t(hashcat compatible sha1 utf16le($pass).$salt)\n" +
"140\t\t(hashcat compatible sha1 $salt.utf16le($pass))\n" +
"170\t\t(hashcat compatible sha1 utf16le($pass))\n" +
"sha224\t\t1300\n" +
"sha224passsalt\t1310\n" +
"sha224saltpass\t1320\n" +
"sha256\t\t1400\n" +
"sha256passsalt\t1410\n" +
"sha256saltpass\t1420\n" +
"1430\t\t(hashcat compatible sha256 utf16le($pass).$salt)\n" +
"1440\t\t(hashcat compatible sha256 $salt.utf16le($pass))\n" +
"1470\t\t(hashcat compatible sha256 utf16le($pass))\n" +
"sha256crypt\t7400 (Linux shadow $5$)\n" +
"sha384\t\t10800\n" +
"sha384passsalt\t10810\n" +
"sha384saltpass\t10820\n" +
"sha512\t\t1700\n" +
"sha512passsalt\t1710\n" +
"sha512saltpass\t1720\n" +
"1730\t\t(hashcat compatible sha512 utf16le($pass).$salt)\n" +
"1740\t\t(hashcat compatible sha512 $salt.utf16le($pass))\n" +
"1770\t\t(hashcat compatible sha512 utf16le($pass))\n" +
"sha512crypt\t1800 (Linux shadow $6$)\n" +
"50\t\t(hashcat compatible HMAC-MD5 key = $pass)\n" +
"60\t\t(hashcat compatible HMAC-MD5 key = $salt)\n" +
"150\t\t(hashcat compatible HMAC-SHA1 key = $pass)\n" +
"160\t\t(hashcat compatible HMAC-SHA1 key = $salt)\n" +
"1450\t\t(hashcat compatible HMAC-SHA256 key = $pass)\n" +
"1460\t\t(hashcat compatible HMAC-SHA256 key = $salt)\n" +
"1750\t\t(hashcat compatible HMAC-SHA512 key = $pass)\n" +
"1760\t\t(hashcat compatible HMAC-SHA512 key = $salt)\n" +
"6050\t\t(hashcat compatible HMAC-RIPEMD160 key = $pass)\n" +
"6060\t\t(hashcat compatible HMAC-RIPEMD160 key = $salt)\n" +
"10900\t\t(hashcat compatible PBKDF2-HMAC-SHA256)\n" +
"11900\t\t(hashcat compatible PBKDF2-HMAC-MD5)\n" +
"12000\t\t(hashcat compatible PBKDF2-HMAC-SHA1)\n" +
"12100\t\t(hashcat compatible PBKDF2-HMAC-SHA512)\n" +
"sha512-224\n" +
"sha512-256\n" +
"sha3-224\t17300\n" +
"sha3-256\t17400\n" +
"sha3-384\t17500\n" +
"sha3-512\t17600\n" +
"keccak-224\t17700\n" +
"keccak-256\t17800\n" +
"keccak-384\t17900\n" +
"keccak-512\t18000\n" +
"scrypt\t\t8900\n" +
"wpbcrypt\t(WordPress bcrypt-HMAC-SHA384)\n" +
"yescrypt\t(Linux shadow $y$)\n"
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", "pbkdf2-sha256", "11900", "pbkdf2-md5", "12000", "pbkdf2-sha1", "12100", "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
}
// 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)
}
// 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 saltRaw == nil || 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 ""
}
for i := 0; i < 16; i++ {
salt[i] = cryptBase64[rb[i]&0x3f]
}
}
saltBytes := salt
keyLen := len(password)
saltLen := len(saltBytes)
a := sha512.New()
a.Write(password)
a.Write(saltBytes)
alt := sha512.New()
alt.Write(password)
alt.Write(saltBytes)
alt.Write(password)
altSum := alt.Sum(nil)
if keyLen > 0 {
n := keyLen / 64
for i := 0; i < n; i++ {
a.Write(altSum)
}
a.Write(altSum[:keyLen%64])
}
for cnt := keyLen; cnt > 0; cnt >>= 1 {
if (cnt & 1) != 0 {
a.Write(altSum)
} else {
a.Write(password)
}
}
final := a.Sum(nil)
dp := sha512.New()
for i := 0; i < keyLen; i++ {
dp.Write(password)
}
dpSum := dp.Sum(nil)
P := make([]byte, keyLen)
for i := 0; i+64 <= keyLen; i += 64 {
copy(P[i:i+64], dpSum)
}
copy(P[(keyLen/64)*64:], dpSum[:keyLen%64])
ds := sha512.New()
reps := 16 + int(final[0])
for i := 0; i < reps; i++ {
ds.Write(saltBytes)
}
dsSum := ds.Sum(nil)
S := make([]byte, saltLen)
for i := 0; i+64 <= saltLen; i += 64 {
copy(S[i:i+64], dsSum)
}
copy(S[(saltLen/64)*64:], dsSum[:saltLen%64])
for i := 0; i < rounds; i++ {
c := sha512.New()
if (i & 1) != 0 {
c.Write(P)
} else {
c.Write(final)
}
if i%3 != 0 {
c.Write(S)
}
if i%7 != 0 {
c.Write(P)
}
if (i & 1) != 0 {
c.Write(final)
} else {
c.Write(P)
}
final = c.Sum(nil)
}
enc := func(dst *[]byte, b2, b1, b0 byte, n int) {
w := uint32(b2)<<16 | uint32(b1)<<8 | uint32(b0)
for i := 0; i < n; i++ {
*dst = append(*dst, cryptBase64[w&0x3f])
w >>= 6
}
}
buf := make([]byte, 0, len(magic)+saltLen+1+86)
buf = append(buf, magic...)
buf = append(buf, saltBytes...)
buf = append(buf, '$')
enc(&buf, final[0], final[21], final[42], 4)
enc(&buf, final[22], final[43], final[1], 4)
enc(&buf, final[44], final[2], final[23], 4)
enc(&buf, final[3], final[24], final[45], 4)
enc(&buf, final[25], final[46], final[4], 4)
enc(&buf, final[47], final[5], final[26], 4)
enc(&buf, final[6], final[27], final[48], 4)
enc(&buf, final[28], final[49], final[7], 4)
enc(&buf, final[50], final[8], final[29], 4)
enc(&buf, final[9], final[30], final[51], 4)
enc(&buf, final[31], final[52], final[10], 4)
enc(&buf, final[53], final[11], final[32], 4)
enc(&buf, final[12], final[33], final[54], 4)
enc(&buf, final[34], final[55], final[13], 4)
enc(&buf, final[56], final[14], final[35], 4)
enc(&buf, final[15], final[36], final[57], 4)
enc(&buf, final[37], final[58], final[16], 4)
enc(&buf, final[59], final[17], final[38], 4)
enc(&buf, final[18], final[39], final[60], 4)
enc(&buf, final[40], final[61], final[19], 4)
enc(&buf, final[62], final[20], final[41], 4)
enc(&buf, 0, 0, final[63], 2)
return string(buf)
}
// WordPress bcrypt: $wp$2y$10$<22-salt><31-hash>
// bcrypt(base64(HMAC-SHA384(key="wp-sha384",$password)))
func wpbcrypt(password []byte, cost int) string {
const (
wpPrefix = "$wp$"
bcryptPrefix = "$2y$"
)
mac := hmac.New(sha512.New384, []byte("wp-sha384"))
mac.Write(password)
tag := mac.Sum(nil)
b64 := make([]byte, base64.StdEncoding.EncodedLen(len(tag)))
base64.StdEncoding.Encode(b64, tag)
bh, err := bcrypt.GenerateFromPassword(b64, cost)
if err != nil {
fmt.Fprintln(os.Stderr, "wpbcrypt error:", err)
return ""
}
s := string(bh)
if len(s) < 4 || s[0] != '$' {
fmt.Fprintln(os.Stderr, "wpbcrypt: unexpected bcrypt format")
return ""
}
s = bcryptPrefix + s[4:]
return wpPrefix + s[1:]
}
// yescrypt, using debian/libxcrypt defaults
func yescryptHash(pass []byte) string {
// debian/libxcrypt defaults: N=4096, r=32, p=1, keyLen=32, 128-bit salt
const N = 4096
const r = 32
const p = 1
const keyLen = 32
const saltLen = 16
// salt
salt := make([]byte, saltLen)
if !readRand(salt) {
fmt.Fprintln(os.Stderr, "yescrypt: salt error")
return ""
}
// derive
key, err := yescrypt.Key(pass, salt, N, r, p, keyLen)
if err != nil {
fmt.Fprintln(os.Stderr, "yescrypt:", err)
return ""
}
// crypt-base64 encoder (./0-9A-Za-z)
encode64 := func(src []byte) string {
var dst []byte
var v uint32
bitsAcc := 0
for i := 0; i < len(src); i++ {
v |= uint32(src[i]) << bitsAcc
bitsAcc += 8
for bitsAcc >= 6 {
dst = append(dst, cryptBase64[v&0x3f])
v >>= 6
bitsAcc -= 6
}