summaryrefslogtreecommitdiff
path: root/tests/reference/bcrypt_reference.go
blob: 81417c88fe6a016f3f9c5928e0609d262c4e44f2 (plain)
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
package main

import (
	"io"
	"os"
	"strconv"

	"golang.org/x/crypto/bcrypt"
)

// provides access to the golang implementation of bcrypt, for reference:
// "password" to hash is provided on stdin, cost parameter is an optional
// command-line parameter

func main() {
	cost := bcrypt.MinCost
	if len(os.Args) > 1 {
		if parsed, err := strconv.Atoi(os.Args[1]); err == nil {
			cost = parsed
		}
	}

	buf, err := io.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}

	out, err := bcrypt.GenerateFromPassword(buf, cost)
	if err != nil {
		panic(err)
	}

	os.Stdout.Write(out)
	os.Stdout.Write([]byte("\n"))
}