ruby和glang的md5和sha1加密对比
ruby和glang的md5和sha1加密对比
package main
import (
"crypto/md5"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"log"
"strings"
)
func main() {
testMd5OrSha2()
}
func testMd5OrSha2() {
//ruby代码sha1加密
//require 'digest/sha2'
//text = Digest::SHA1.hexdigest("Hello world")
str := "hello world!"
// golang代码sha1加密
s := sha1.New()
s.Write([]byte(str))
sha1str1 := fmt.Sprintf("%x", s.Sum(nil)) //将[]byte转成16进制
fmt.Println(sha1str1)
//ruby代码MD5加密
//Digest::MD5.hexdigest("#{appid}:#{key}:#{timestamp}")
//golang代码MD5加密 方法一
data := []byte(str)
has := md5.Sum(data)
md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制
fmt.Println(md5str1)
//golang代码MD5加密 方法二
w := md5.New()
_, _ = io.WriteString(w, str)
//将str写入到w中
md5str2 := fmt.Sprintf("%x", w.Sum(nil))
fmt.Println(md5str2)
}
[Haima的博客]
http://www.cnblogs.com/haima/