-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (55 loc) · 1.35 KB
/
main.go
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
package main
import (
"bufio"
"encoding/base64"
"flag"
"fmt"
"log"
"os"
"github.com/kevinnoel-be/hasher/pkg/hash"
)
type args struct {
iterations int
salt string
privateSalt string
}
func main() {
a := args{}
flag.IntVar(&a.iterations, "iterations", 1, "Number of iterations")
flag.StringVar(&a.salt, "salt", "", "Base 64 encoded salt (public)")
flag.StringVar(&a.privateSalt, "private-salt", "", "Base 64 encoded private salt")
flag.Parse()
var password []byte
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
password = append(password, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read pipe data: %v", err)
}
} else {
fmt.Print("Data to hash: ")
var pass string
_, _ = fmt.Scanf("%s", &pass)
password = []byte(pass)
}
if len(password) == 0 {
log.Fatal("Empty data")
}
computedHash, _ := hash.Compute(hash.Request{
Data: password,
Salt: b64decode(a.salt),
PrivateSalt: b64decode(a.privateSalt),
Iterations: a.iterations,
})
fmt.Printf("%v\n", base64.StdEncoding.EncodeToString(computedHash))
}
func b64decode(s string) []byte {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
log.Fatalf("Failed to base64 decode: %v", err)
}
return decoded
}