1. kou_pg_0131

    No comment

    kou_pg_0131
Changes in body
Source | HTML | Preview
@@ -1,61 +1,60 @@
## 修正
RIPEMD160アルゴリズムのハッシュ値の取得方法が誤っていたので、修正しました。(2019/09/19 12:45)
## 関連
- [【Ruby】文字列のハッシュ値を取得(SHA, MD5, RMD160) - Qiita](https://qiita.com/kou_pg_0131/items/ad7a0fdd7b0a997cd5f4)
- [【Node.js】文字列のハッシュ値を取得(SHA, MD5, RMD160) - Qiita](https://qiita.com/kou_pg_0131/items/174aefd8f894fea4d11a)
## サンプルコード
```go
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"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)
- rip := ripemd160.New()
- io.WriteString(rip, s)
- rmd160 := rip.Sum(nil)
+ rmd160 := ripemd160.New()
+ io.WriteString(rmd160, s)
// 出力したいだけなら`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(rmd160))
-
- // => md5: 5f4dcc3b5aa765d61d8327deb882cf99
- // sha1: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
- // sha256: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
- // sha384: a8b64babd0aca91a59bdbb7761b421d4f2bb38280d3a75ba0f21f2bebc45583d446c598660c94ce680c47d19c30783a7
- // sha512: b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86
- // ripemd160: 2c08e8f5884750a7b99f6f2f342fc638db25ff31
+ 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("rmd160: " + hex.EncodeToString(rmd160.Sum(nil)))
+
+ // => md5: 5f4dcc3b5aa765d61d8327deb882cf99
+ // sha1: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
+ // sha256: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
+ // sha384: a8b64babd0aca91a59bdbb7761b421d4f2bb38280d3a75ba0f21f2bebc45583d446c598660c94ce680c47d19c30783a7
+ // sha512: b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86
+ // rmd160: 2c08e8f5884750a7b99f6f2f342fc638db25ff31
}
```
## 参考
- [GolangでSHA-256ハッシュ - Qiita](https://qiita.com/curious-eyes/items/abb29e1a06ac5bc2c5f2)
- [crypto - The Go Programming Language](https://golang.org/pkg/crypto/)