Skip to content

Commit 549e3fe

Browse files
authored
Merge pull request #168 from devlights/add-bcrypt-example-#167
Add bcrypt package example
2 parents e8c13a1 + b00b329 commit 549e3fe

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package crypto
2+
3+
import (
4+
"github.com/devlights/try-golang/lib/output"
5+
"golang.org/x/crypto/bcrypt"
6+
"golang.org/x/crypto/ssh/terminal"
7+
"syscall"
8+
)
9+
10+
// BcryptPasswordHash は、 bcrypt パッケージを利用してパスワードのハッシュ化をしてみるサンプルです.
11+
// パスワードの読み取りには、 terminal パッケージの ReadPassword() を利用しています.
12+
//
13+
// REFERENCES::
14+
// - https://medium.com/@jcox250/password-hash-salt-using-golang-b041dc94cb72
15+
// - https://pkg.go.dev/golang.org/x/crypto/bcrypt?tab=doc
16+
// - https://pkg.go.dev/golang.org/x/crypto/ssh/terminal?tab=doc
17+
// - https://ja.wikipedia.org/wiki/Bcrypt
18+
// - https://liginc.co.jp/377191
19+
// - https://stackoverflow.com/questions/30363790/silence-user-input-in-scan-function
20+
func BcryptPasswordHash() error {
21+
22+
// パスワード読み取り
23+
password, err := readPassword()
24+
if err != nil {
25+
return err
26+
}
27+
28+
// bcrypt で ハッシュ化
29+
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
30+
if err != nil {
31+
return err
32+
}
33+
34+
// ハッシュ化したものを文字列で表示
35+
output.Stdoutl("Hashed Password: ", string(hashedPassword))
36+
37+
// 再度パスワード読み取り
38+
password2, err := readPassword()
39+
if err != nil {
40+
return err
41+
}
42+
43+
// 一致するかを bcrypt パッケージの関数で確認
44+
err = bcrypt.CompareHashAndPassword(hashedPassword, password2)
45+
if err != nil {
46+
output.Stdoutl("CompareHashAndPassword", "一致しませんでした")
47+
} else {
48+
output.Stdoutl("CompareHashAndPassword", "一致しました")
49+
}
50+
51+
return nil
52+
}
53+
54+
func readPassword() ([]byte, error) {
55+
56+
output.Stdoutf("ENTER Password: ", "")
57+
58+
password, err := terminal.ReadPassword(int(syscall.Stdin))
59+
if err != nil {
60+
return nil, err
61+
}
62+
output.Stdoutl("", "")
63+
64+
return password, nil
65+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require (
77
github.com/cheekybits/genny v1.0.0
88
github.com/deckarep/golang-set v1.7.1
99
github.com/getsentry/sentry-go v0.4.0
10+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
1011
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDf
142142
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
143143
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
144144
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
145+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
145146
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
146147
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
147148
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -158,6 +159,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
158159
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
159160
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
160161
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
162+
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
161163
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
162164
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
163165
golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

lib/mapping.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lib
33
import (
44
"github.com/devlights/try-golang/advanced/async"
55
"github.com/devlights/try-golang/advanced/closure"
6+
"github.com/devlights/try-golang/advanced/crypto"
67
"github.com/devlights/try-golang/advanced/generate"
78
"github.com/devlights/try-golang/advanced/reflection"
89
"github.com/devlights/try-golang/advanced/sets"
@@ -147,6 +148,7 @@ func (m SampleMapping) MakeMapping() {
147148
m["xdg_base_directory"] = xdgspec.XdgBaseDirectory
148149
m["xdg_user_directory"] = xdgspec.XdgUserDirectory
149150
m["xdg_file_operation"] = xdgspec.XdgFileOperation
151+
m["crypto_bcrypt_password_hash"] = crypto.BcryptPasswordHash
150152

151153
m["tutorial_gotour_helloworld"] = tutorial.HelloWorld
152154
m["tutorial_gotour_import"] = tutorial.Import

0 commit comments

Comments
 (0)