Skip to content

Commit ef8a91b

Browse files
v2022-12-24.1800-optimize
v2022-12-24.1800-optimize; optimized all hashing functions, tweaked buffer size
1 parent d53e5c2 commit ef8a91b

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

hashgen.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/sha512"
99
"encoding/base64"
1010
"encoding/binary"
11+
"encoding/hex"
1112
"unicode/utf16"
1213
"flag"
1314
"fmt"
@@ -36,16 +37,17 @@ import (
3637
// v2022-12-20.1430-goroutine; complete rewrite using goroutines & read/write buffers
3738
// v2022-12-21.1400-goroutine; added multiple new algo's including hashcat mode equivalents
3839
// v2022-12-23.1200-goroutine; added argon2id (very slow), added sync / wait group for future use, change h/s readout from millions to thousands,
40+
// v2022-12-24.1800-optimize; optimized all hashing functions, tweaked buffer size
3941

4042
// TODO:
4143
// continue to add more hash functions
4244
// add "-o stdout" flag to print hashes to stdout
43-
// optimize hash functions for better performance (ex: ntlm is currently slower than md5)
45+
// continue optimizing hash functions for better performance (ex: ntlm is currently slower than md5)
4446
// fine tune goroutines for better performance with read --> hash function --> write
4547
// add feature to allow hash cracking in addition to hash generation (future program: hashgenie)
4648

4749
func versionFunc() {
48-
funcBase64Decode("Q3ljbG9uZSBoYXNoIGdlbmVyYXRvciB2MjAyMi0xMi0yMy4xMjAwLWdvcm91dGluZQo=")
50+
funcBase64Decode("Q3ljbG9uZSBoYXNoIGdlbmVyYXRvciB2MjAyMi0xMi0yNC4xODAwLW9wdGltaXplCg==")
4951
}
5052

5153
// help function
@@ -82,7 +84,7 @@ func helpFunc() {
8284
"sha3-512 \t 17400\n"
8385
fmt.Println(str)
8486
os.Exit(0)
85-
}
87+
}
8688

8789
// main function
8890
func main() {
@@ -201,18 +203,25 @@ func main() {
201203
}
202204

203205
// create read / write buffers
206+
// input buffer
204207
inputBuffer := bufio.NewScanner(input)
205-
outputBuffer := bufio.NewWriter(output)
208+
// set buffer capacity to 10mb
209+
const maxCapacity = 10*1024*1024
210+
buf := make([]byte, maxCapacity)
211+
inputBuffer.Buffer(buf, maxCapacity)
212+
// set output buffer to 100mb
213+
outputBuffer := bufio.NewWriterSize(output, 100*1024*1024)
206214

207215
// create buffered channels to receive lines <-- not currently emplimented
208-
//lines := make(chan string, 1024)
209-
//linesHash := make(chan string, 1024)
216+
//lines := make(chan string, 10*1024*1024)
217+
//linesHash := make(chan string, 10*1024*1024)
210218

211219
// create WaitGroup to wait for goroutines to finish
212220
var wg sync.WaitGroup
213221

214222
// create goroutine bool channel
215223
done := make(chan bool)
224+
// start hashgen goroutine
216225
wg.Add(1)
217226
go func() {
218227
defer wg.Done()
@@ -226,12 +235,12 @@ func main() {
226235
for inputBuffer.Scan() {
227236
line := inputBuffer.Text()
228237
pwd := []byte(line)
229-
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost ) // <-- cost can be adjusted here
238+
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) // <-- cost can be adjusted here
230239
if err != nil {
231240
log.Println(err)
232241
}
233242
hashString := string(hash)
234-
outputBuffer.WriteString(fmt.Sprintf("%v\n", hashString))
243+
outputBuffer.WriteString(string(hashString) + "\n")
235244
linesHashed++
236245
}
237246
} else if hashFunc == "argon2id" {
@@ -250,7 +259,7 @@ func main() {
250259
log.Fatal(err)
251260
}
252261
// Print argon2id hash
253-
outputBuffer.WriteString(fmt.Sprintf("%v\n", hash))
262+
outputBuffer.WriteString(string(hash) + "\n")
254263
linesHashed++
255264
}
256265
} else if hashFunc == "blake2b-256" {
@@ -261,7 +270,7 @@ func main() {
261270
hash, _ := blake2b.New256(nil)
262271
hash.Write(lineByte)
263272
hashValue := hash.Sum(nil)
264-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hashValue))
273+
outputBuffer.WriteString(hex.EncodeToString(hashValue) + "\n")
265274
linesHashed++
266275
}
267276
} else if hashFunc == "blake2b-384" {
@@ -272,7 +281,7 @@ func main() {
272281
hash, _ := blake2b.New384(nil)
273282
hash.Write(lineByte)
274283
hashValue := hash.Sum(nil)
275-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hashValue))
284+
outputBuffer.WriteString(hex.EncodeToString(hashValue) + "\n")
276285
linesHashed++
277286
}
278287
} else if hashFunc == "blake2b-512" {
@@ -283,7 +292,7 @@ func main() {
283292
hash, _ := blake2b.New512(nil)
284293
hash.Write(lineByte)
285294
hashValue := hash.Sum(nil)
286-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hashValue))
295+
outputBuffer.WriteString(hex.EncodeToString(hashValue) + "\n")
287296
linesHashed++
288297
}
289298
} else if hashFunc == "blake2s-256" {
@@ -294,7 +303,7 @@ func main() {
294303
hash, _ := blake2s.New256(nil)
295304
hash.Write(lineByte)
296305
hashValue := hash.Sum(nil)
297-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hashValue))
306+
outputBuffer.WriteString(hex.EncodeToString(hashValue) + "\n")
298307
linesHashed++
299308
}
300309
} else if hashFunc == "crc32" {
@@ -303,7 +312,7 @@ func main() {
303312
line := inputBuffer.Text()
304313
h := crc32.ChecksumIEEE([]byte(line))
305314
hash := strconv.FormatUint(uint64(h), 16)
306-
outputBuffer.WriteString(fmt.Sprintf("%v\n", hash))
315+
outputBuffer.WriteString(string(hash) + "\n")
307316
linesHashed++
308317
}
309318
} else if hashFunc == "crc64" {
@@ -313,15 +322,16 @@ func main() {
313322
password := []byte(line)
314323
table := crc64.MakeTable(crc64.ECMA)
315324
hash := crc64.Checksum(password, table)
316-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hash))
325+
hashString := strconv.FormatUint(hash, 16)
326+
outputBuffer.WriteString(hashString + "\n")
317327
linesHashed++
318-
}
328+
}
319329
} else if hashFunc == "base64encode" {
320330
// base64encode
321331
for inputBuffer.Scan() {
322332
line := inputBuffer.Text()
323333
str := base64.StdEncoding.EncodeToString([]byte(line))
324-
outputBuffer.WriteString(fmt.Sprintf("%v\n", str))
334+
outputBuffer.WriteString(string(str) + "\n")
325335
linesHashed++
326336
}
327337
} else if hashFunc == "base64decode" {
@@ -331,9 +341,9 @@ func main() {
331341
str, err := base64.StdEncoding.DecodeString(line)
332342
if err != nil {
333343
fmt.Println("--> Text doesn't appear to be base64 encoded. <--")
334-
//os.Exit(0) // <-- uncomment this line to stop on error or leave commented to skip non-base64 lines and continue
344+
os.Exit(0) // <-- uncomment this line to stop on error or leave commented to skip non-base64 lines and continue
335345
}
336-
outputBuffer.WriteString(fmt.Sprintf("%s\n", str))
346+
outputBuffer.WriteString(string(str) + "\n")
337347
linesHashed++
338348
}
339349
} else if hashFunc == "ntlm" {
@@ -345,31 +355,30 @@ func main() {
345355
if err := binary.Write(hash, binary.LittleEndian, input); err != nil {
346356
panic(fmt.Errorf("--> Failed NTLM hashing: %w <--", err))
347357
}
348-
outputHash := hash.Sum(nil)
349-
outputBuffer.WriteString(fmt.Sprintf("%X\n", outputHash))
358+
outputBuffer.WriteString(hex.EncodeToString(hash.Sum(nil)) + "\n")
350359
linesHashed++
351360
}
352361
} else if hashFunc == "plaintext" {
353362
// print plaintext
354363
for inputBuffer.Scan() {
355364
line := inputBuffer.Text()
356-
outputBuffer.WriteString(fmt.Sprintf("%v\n", line))
365+
outputBuffer.WriteString(string(line) + "\n")
357366
linesHashed++
358367
}
359368
} else if hashFunc == "sha2-224" {
360-
// sha224 hash function
369+
// sha2_224 hash function
361370
for inputBuffer.Scan() {
362371
line := inputBuffer.Text()
363372
hash := sha256.Sum224([]byte(line))
364-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hash))
373+
outputBuffer.WriteString(hex.EncodeToString(hash[:]) + "\n")
365374
linesHashed++
366375
}
367376
} else if hashFunc == "sha2-384" {
368-
// sha384 hash function
377+
// sha2_384 hash function
369378
for inputBuffer.Scan() {
370379
line := inputBuffer.Text()
371380
hash := sha512.Sum384([]byte(line))
372-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hash))
381+
outputBuffer.WriteString(hex.EncodeToString(hash[:]) + "\n")
373382
linesHashed++
374383
}
375384
} else { // other hash functions defined in switch
@@ -378,7 +387,7 @@ func main() {
378387
h.Reset()
379388
h.Write([]byte(line))
380389
hash := h.Sum(nil)
381-
outputBuffer.WriteString(fmt.Sprintf("%x\n", hash))
390+
outputBuffer.WriteString(hex.EncodeToString(hash) + "\n")
382391
linesHashed++
383392
}
384393
}

0 commit comments

Comments
 (0)