bcrypt.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package bcrypt
  2. import (
  3. "crypto"
  4. "crypto/hmac"
  5. "crypto/md5"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/sha1"
  9. "crypto/sha256"
  10. "crypto/x509"
  11. "encoding/base64"
  12. "encoding/hex"
  13. "encoding/pem"
  14. "errors"
  15. "fmt"
  16. "github.com/druidcaesa/gotool/logs"
  17. "golang.org/x/crypto/bcrypt"
  18. )
  19. type BcryptUtil struct {
  20. logs logs.Logs
  21. }
  22. // Generate Password encryption 密码加密
  23. func (b *BcryptUtil) Generate(password string) string {
  24. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) //加密处理
  25. if err != nil {
  26. b.logs.ErrorLog().Println(err)
  27. }
  28. return string(hash)
  29. }
  30. // CompareHash Password validation 密码验证
  31. func (b *BcryptUtil) CompareHash(dbPassword string, loginPassword string) bool {
  32. err := bcrypt.CompareHashAndPassword([]byte(dbPassword), []byte(loginPassword))
  33. if err != nil {
  34. b.logs.ErrorLog().Println(err)
  35. return false
  36. }
  37. return true
  38. }
  39. // MD5 md5签名 signature
  40. func (b *BcryptUtil) MD5(s string) string {
  41. data := []byte(s)
  42. md5Ctx := md5.New()
  43. md5Ctx.Write(data)
  44. cipherStr := md5Ctx.Sum(nil)
  45. return hex.EncodeToString(cipherStr)
  46. }
  47. // SHA1 sha1加密 encryption
  48. func (b *BcryptUtil) SHA1(s string) string {
  49. o := sha1.New()
  50. o.Write([]byte(s))
  51. return hex.EncodeToString(o.Sum(nil))
  52. }
  53. // ComputeHmacSha256 hmac_sha256 encryption
  54. func (b *BcryptUtil) ComputeHmacSha256(message string, secret string) string {
  55. key := []byte(secret)
  56. h := hmac.New(sha256.New, key)
  57. h.Write([]byte(message))
  58. sha := hex.EncodeToString(h.Sum(nil))
  59. return base64.StdEncoding.EncodeToString([]byte(sha))
  60. }
  61. // GenRsaKey RSA公钥私钥产生
  62. func (b *BcryptUtil) GenRsaKey() (prvkey, pubkey []byte) {
  63. // 生成私钥文件
  64. privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
  65. if err != nil {
  66. panic(err)
  67. }
  68. derStream := x509.MarshalPKCS1PrivateKey(privateKey)
  69. block := &pem.Block{
  70. Type: "RSA PRIVATE KEY",
  71. Bytes: derStream,
  72. }
  73. prvkey = pem.EncodeToMemory(block)
  74. publicKey := &privateKey.PublicKey
  75. derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
  76. if err != nil {
  77. panic(err)
  78. }
  79. block = &pem.Block{
  80. Type: "PUBLIC KEY",
  81. Bytes: derPkix,
  82. }
  83. pubkey = pem.EncodeToMemory(block)
  84. return
  85. }
  86. // RsaSignWithSha256 签名
  87. func (b *BcryptUtil) RsaSignWithSha256(data []byte, keyBytes []byte) []byte {
  88. h := sha256.New()
  89. h.Write(data)
  90. hashed := h.Sum(nil)
  91. block, _ := pem.Decode(keyBytes)
  92. if block == nil {
  93. panic(errors.New("private key error"))
  94. }
  95. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  96. if err != nil {
  97. fmt.Println("ParsePKCS8PrivateKey err", err)
  98. panic(err)
  99. }
  100. signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
  101. if err != nil {
  102. fmt.Printf("Error from signing: %s\n", err)
  103. panic(err)
  104. }
  105. return signature
  106. }
  107. // RsaVerySignWithSha256 验证
  108. func (b *BcryptUtil) RsaVerySignWithSha256(data, signData, keyBytes []byte) bool {
  109. block, _ := pem.Decode(keyBytes)
  110. if block == nil {
  111. panic(errors.New("public key error"))
  112. }
  113. pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
  114. if err != nil {
  115. panic(err)
  116. }
  117. hashed := sha256.Sum256(data)
  118. err = rsa.VerifyPKCS1v15(pubKey.(*rsa.PublicKey), crypto.SHA256, hashed[:], signData)
  119. if err != nil {
  120. panic(err)
  121. }
  122. return true
  123. }
  124. // RsaEncrypt 公钥加密
  125. func (b *BcryptUtil) RsaEncrypt(data, keyBytes []byte) []byte {
  126. //解密pem格式的公钥
  127. block, _ := pem.Decode(keyBytes)
  128. if block == nil {
  129. panic(errors.New("public key error"))
  130. }
  131. // 解析公钥
  132. pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
  133. if err != nil {
  134. panic(err)
  135. }
  136. // 类型断言
  137. pub := pubInterface.(*rsa.PublicKey)
  138. //加密
  139. ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, pub, data)
  140. if err != nil {
  141. panic(err)
  142. }
  143. return ciphertext
  144. }
  145. // RsaDecrypt 私钥解密
  146. func (b *BcryptUtil) RsaDecrypt(ciphertext, keyBytes []byte) []byte {
  147. //获取私钥
  148. block, _ := pem.Decode(keyBytes)
  149. if block == nil {
  150. panic(errors.New("private key error!"))
  151. }
  152. //解析PKCS1格式的私钥
  153. priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  154. if err != nil {
  155. panic(err)
  156. }
  157. // 解密
  158. data, err := rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
  159. if err != nil {
  160. panic(err)
  161. }
  162. return data
  163. }