在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 main

import (
	"crypto/rand"
	"fmt"
	"golang.org/x/crypto/scrypt"
	"io"
	"log"
)

const (
	PW_SALT_BYTES = 8
	PW_HASH_BYTES = 32
	PASS_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 参考链接

  1. equivalent salt and hash in golang
  2. example_test.go

你可能感兴趣的:(golang)