cert.go 943 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package utils
  2. import (
  3. "crypto/rand"
  4. "crypto/sha256"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "encoding/pem"
  9. )
  10. func CheckHttps(pub string) (*x509.Certificate, error) {
  11. skBlock, _ := pem.Decode([]byte(pub))
  12. cert, err := x509.ParseCertificate(skBlock.Bytes)
  13. return cert, err
  14. }
  15. func GetSHA256HashCode(stringMessage string) string {
  16. message := []byte(stringMessage) //字符串转化字节数组
  17. //创建一个基于SHA256算法的hash.Hash接口的对象
  18. hash := sha256.New() //sha-256加密
  19. //hash := sha512.New() //SHA-512加密
  20. //输入数据
  21. hash.Write(message)
  22. //计算哈希值
  23. bytes := hash.Sum(nil)
  24. //将字符串编码为16进制格式,返回字符串
  25. hashCode := hex.EncodeToString(bytes)
  26. //返回哈希值
  27. return hashCode
  28. }
  29. func RandPassword(n int) (string, error) {
  30. b := make([]byte, n)
  31. _, err := rand.Read(b)
  32. if err != nil {
  33. return "", err
  34. }
  35. return base64.StdEncoding.EncodeToString(b), nil
  36. }