1. kou_pg_0131

    Posted

    kou_pg_0131
Changes in title
+【Go】文字列をハッシュ化
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,54 @@
+## 関連
+
+- [【Ruby】文字列をハッシュ化 - Qiita](https://qiita.com/kou_pg_0131/items/ad7a0fdd7b0a997cd5f4)
+- [【Node.js】文字列をハッシュ化 - Qiita](https://qiita.com/kou_pg_0131/items/174aefd8f894fea4d11a)
+
+## サンプルコード
+
+```go
+package main
+
+import (
+ "crypto/md5"
+ "crypto/sha1"
+ "crypto/sha256"
+ "crypto/sha512"
+ "encoding/hex"
+ "fmt"
+
+ "golang.org/x/crypto/ripemd160"
+)
+
+func main() {
+ s := "password"
+ b := []byte(s)
+
+ md5 := md5.Sum(b)
+ sha1 := sha1.Sum(b)
+ sha256 := sha256.Sum256(b)
+ sha384 := sha512.Sum384(b)
+ sha512 := sha512.Sum512(b)
+ ripemd160 := ripemd160.New().Sum(b)
+
+ // 出力したいだけなら`fmt.Printf("%x\n", md5)`とすればよい
+ // 文字列として取得したい場合は`hex.EncodeToString`を使用する
+ fmt.Println("md5: " + hex.EncodeToString(md5[:]))
+ fmt.Println("sha1: " + hex.EncodeToString(sha1[:]))
+ fmt.Println("sha256: " + hex.EncodeToString(sha256[:]))
+ fmt.Println("sha384: " + hex.EncodeToString(sha384[:]))
+ fmt.Println("sha512: " + hex.EncodeToString(sha512[:]))
+ fmt.Println("ripemd160: " + hex.EncodeToString(ripemd160[:]))
+
+ // => md5: 5f4dcc3b5aa765d61d8327deb882cf99
+ // sha1: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
+ // sha256: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
+ // sha384: a8b64babd0aca91a59bdbb7761b421d4f2bb38280d3a75ba0f21f2bebc45583d446c598660c94ce680c47d19c30783a7
+ // sha512: b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86
+ // ripemd160: 70617373776f72649c1185a5c5e9fc54612808977ee8f548b2258d31
+}
+```
+
+## 参考
+
+- [GolangでSHA-256ハッシュ - Qiita](https://qiita.com/curious-eyes/items/abb29e1a06ac5bc2c5f2)
+- [crypto - The Go Programming Language](https://golang.org/pkg/crypto/)