💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 常用加密 ### 密码加密 ```go package main import ( "errors" "fmt" "github.com/prometheus/common/log" "golang.org/x/crypto/bcrypt" ) //GeneratePassword 加密明文密码 func GeneratePassword(pwd string) (string, error) { encryptedPwd, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost) return string(encryptedPwd), err } //ValidatePassword 验证密码 // pwd 明文密码 // encryptedPwd 加密的密码 func ValidatePassword(pwd string, encryptedPwd string) (bool, error) { if err := bcrypt.CompareHashAndPassword([]byte(encryptedPwd), []byte(pwd)); err != nil { return false, errors.New("wrong password") } return true, nil } func main() { p := "123456" pwd, err := GeneratePassword(p) if err != nil { log.Error(err) } fmt.Println("ciphertext:", pwd) isOk, _ := ValidatePassword(p, pwd) if !isOk { log.Error(err) } fmt.Println(isOk) } ``` 打印结果 ```bash ciphertext: $2a$10$mGB.ml/M/2A..NgIrFSk1ekl0Qi3gIvfk3n0sOFTje9ScZtxxpLfG true ``` ### MD5 ```go package main import ( "crypto/md5" "fmt" "io" ) func Md5(str string) string { h := md5.New() _, _ = io.WriteString(h, str) return fmt.Sprintf("%x", h.Sum(nil)) } func main() { fmt.Println(Md5("123456")) } ``` 打印结果 ```bash e10adc3949ba59abbe56e057f20f883e ``` ### SHA ```go package main import ( "crypto/sha1" "crypto/sha256" "fmt" "io" ) func SHA1(str string) string { h := sha1.New() _, _ = io.WriteString(h, str) return fmt.Sprintf("%x", h.Sum(nil)) } func SHA2(str string) string { h := sha256.New() _, _ = io.WriteString(h, str) return fmt.Sprintf("%x", h.Sum(nil)) } func main() { msg := "https://golang.org" fmt.Println(SHA1(msg)) fmt.Println(SHA2(msg)) } ``` 打印结果 ```bash 0a95b510a2e68b88ebc9bc09e76f7dbcfa1eb90e bfd04cc20536303bd2c62bad327217f5c94f96578a5e95ca495f1d46c113016b ``` ### BASE64 ```go package main import ( "encoding/base64" "fmt" ) func Base64Encode(str string) string { encoded := base64.StdEncoding.EncodeToString([]byte(str)) return encoded } func Base64Decode(str string) (string, error) { decoded, err := base64.StdEncoding.DecodeString(str) if err != nil { return "", err } return string(decoded), nil } func main() { msg := "https://golang.org" fmt.Println(Base64Encode(msg)) fmt.Println(Base64Decode("aHR0cHM6Ly9nb2xhbmcub3Jn")) } ``` ### AES加解密 ```go package main import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "errors" "fmt" ) //高级加密标准(Adevanced Encryption Standard ,AES) //16,24,32位字符串的话,分别对应AES-128,AES-192,AES-256 加密方法 //key不能泄露 var PwdKey = []byte("DIS**#KKKDJJSKDI") //PKCS7 填充模式 func PKCS7Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize //Repeat()函数的功能是把切片[]byte{byte(padding)}复制padding个,然后合并成新的字节切片返回 padtext := bytes.Repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } //填充的反向操作,删除填充字符串 func PKCS7UnPadding(origData []byte) ([]byte, error) { //获取数据长度 length := len(origData) if length == 0 { return nil, errors.New("加密字符串错误!") } else { //获取填充字符串长度 unpadding := int(origData[length-1]) //截取切片,删除填充字节,并且返回明文 return origData[:(length - unpadding)], nil } } //实现加密 func AesEcrypt(origData []byte, key []byte) ([]byte, error) { //创建加密算法实例 block, err := aes.NewCipher(key) if err != nil { return nil, err } //获取块的大小 blockSize := block.BlockSize() //对数据进行填充,让数据长度满足需求 origData = PKCS7Padding(origData, blockSize) //采用AES加密方法中CBC加密模式 blocMode := cipher.NewCBCEncrypter(block, key[:blockSize]) crypted := make([]byte, len(origData)) //执行加密 blocMode.CryptBlocks(crypted, origData) return crypted, nil } //实现解密 func AesDeCrypt(cypted []byte, key []byte) ([]byte, error) { //创建加密算法实例 block, err := aes.NewCipher(key) if err != nil { return nil, err } //获取块大小 blockSize := block.BlockSize() //创建加密客户端实例 blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) origData := make([]byte, len(cypted)) //这个函数也可以用来解密 blockMode.CryptBlocks(origData, cypted) //去除填充字符串 origData, err = PKCS7UnPadding(origData) if err != nil { return nil, err } return origData, err } //加密base64 func EnPwdCode(pwd []byte) (string, error) { result, err := AesEcrypt(pwd, PwdKey) if err != nil { return "", err } return base64.StdEncoding.EncodeToString(result), err } //解密 func DePwdCode(pwd string) ([]byte, error) { //解密base64字符串 pwdByte, err := base64.StdEncoding.DecodeString(pwd) if err != nil { return nil, err } //执行AES解密 return AesDeCrypt(pwdByte, PwdKey) } func main() { str := []byte("12fff我是ww.topgoer.com的站长枯藤") pwd, _ := EnPwdCode(str) bytes, _ := DePwdCode(pwd) fmt.Println(string(bytes)) } ```