–
base64加密
相关函数
encoding/base64 Package base64 implements base64 encoding as specified by RFC 4648. StdEncoding StdEncoding is the standard base64 encoding, as defined in RFC 4648. func NewDecoder(enc *Encoding, r io.Reader) io.Reader NewDecoder constructs a new base64 stream decoder. func (enc *Encoding) EncodeToString(src []byte) string EncodeToString returns the base64 encoding of src.
应用:
package main import ( "encoding/base64" "fmt" ) func main() { sEnc := "hellow the world, make it a better place" encodeStd := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" s64 := base64.NewEncoding(encodeStd).EncodeToString([]byte(sEnc)) fmt.Println("encode") fmt.Println(len(sEnc)) fmt.Println(len(s64)) fmt.Println(sEnc) fmt.Println(s64) sDec,_ := base64.StdEncoding.DecodeString(s64) fmt.Println("decode") fmt.Println(len(s64)) fmt.Println(len(sDec)) fmt.Println(s64) fmt.Println(string(sDec)) } D:\go\test2>go run main.go encode 40 56 hellow the world, make it a better place aGVsbG93IHRoZSB3b3JsZCwgbWFrZSBpdCBhIGJldHRlciBwbGFjZQ== decode 56 40 aGVsbG93IHRoZSB3b3JsZCwgbWFrZSBpdCBhIGJldHRlciBwbGFjZQ== hellow the world, make it a better place
sha256哈希和加密
应用
package main import ( "crypto/hmac" "crypto/sha256" "fmt" "io" ) func getHmacCode(s string) string { h := hmac.New(sha256.New, []byte("ourkey")) io.WriteString(h, s) return fmt.Sprintf("%x", h.Sum(nil)) } func getSha256Code(s string) string { h := sha256.New() h.Write([]byte(s)) return fmt.Sprintf("%x", h.Sum(nil)) } func main() { c := getSha256Code("abcdef") fmt.Println(c) c = getHmacCode("abcdef") fmt.Println(c) } D:\go\test2>go run main.go bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721 e244a8e4926bed5a162d3a7ff441a94b2d8e9215262552d909caf6f33e090007
可在下面网站查验:https://1024tools.com/hash
参考文档:
http://blog.csdn.net/wangshubo1989/article/details/72842547
http://blog.csdn.net/wangshubo1989/article/details/74529333
–
–
–
评论前必须登录!
注册