本文主要是介绍在Golang中如何用scrypt对敏感信息加密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0 依赖库
mkdir -p $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/crypto.git
1 源码
package mainimport ("crypto/rand""fmt""golang.org/x/crypto/scrypt""io""log"
)const (PW_SALT_BYTES = 8PW_HASH_BYTES = 32PASS_WORD = "hello scrypt"
)func main() {salt := make([]byte, PW_SALT_BYTES)_, err := io.ReadFull(rand.Reader, salt)if err != nil {log.Fatal(err)} else {fmt.Printf("salt=%v\n", salt)}hash, err := scrypt.Key([]byte(PASS_WORD), salt, 1<<15, 8, 1, PW_HASH_BYTES)if err != nil {log.Fatal(err)}fmt.Printf("encrypt=%x\n", hash)
}
2 加密输出信息
类似
salt=[199 154 209 249 70 17 5 214]
encrypt=80a6b84cb82f3c40fa86a2db787dfb4680e910aa21a1842644363f3903f97e61
3 参考链接
- equivalent salt and hash in golang
- example_test.go
这篇关于在Golang中如何用scrypt对敏感信息加密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!