Help us understand the problem. What is going on with this article?

【Go】文字列のハッシュ値を取得(SHA, MD5, RMD160)

修正

RIPEMD160アルゴリズムのハッシュ値の取得方法が誤っていたので、修正しました。(2019/09/19 12:45)

関連

サンプルコード

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)

    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("rmd160: " + hex.EncodeToString(rmd160.Sum(nil)))

    // => md5:    5f4dcc3b5aa765d61d8327deb882cf99
    //    sha1:   5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
    //    sha256: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
    //    sha384: a8b64babd0aca91a59bdbb7761b421d4f2bb38280d3a75ba0f21f2bebc45583d446c598660c94ce680c47d19c30783a7
    //    sha512: b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86
    //    rmd160: 2c08e8f5884750a7b99f6f2f342fc638db25ff31
}

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away