ZBLOG

golang HS,RS,ES,ED签名与验证

在Golang中,我们可以使用第三方库来进行HS、RS、ES和ED签名与验证。

  1. HS(HMAC-SHA)签名与验证

对于HS签名算法,我们可以使用crypto/hmac包生成签名,并使用常规的字符串比较来验证签名。下面是示例代码:

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "fmt"
  7. )
  8. func generateHMAC(key []byte, data []byte) ([]byte, error) {
  9. h := hmac.New(sha256.New, key)
  10. _, err := h.Write(data)
  11. if err != nil {
  12. return nil, err
  13. }
  14. return h.Sum(nil), nil
  15. }
  16. func verifyHMAC(key []byte, data []byte, signature []byte) bool {
  17. generatedSig, _ := generateHMAC(key, data)
  18. return hmac.Equal(signature, generatedSig)
  19. }
  20. func main() {
  21. key := []byte("mykey")
  22. data := []byte("hello world")
  23. // 签名数据
  24. signature, err := generateHMAC(key, data)
  25. if err != nil {
  26. panic(err)
  27. }
  28. fmt.Println(base64.StdEncoding.EncodeToString(signature))
  29. // 验证签名
  30. isValid := verifyHMAC(key, data, signature)
  31. fmt.Println(isValid)
  32. }

在上面的代码中,我们使用了crypto/hmac包生成了一个sha256哈希值的HMAC-SHA签名,并将其编码为base64字符串输出。然后我们又定义了一个函数用于验证该签名是否有效。

  1. RS(RSA)签名与验证

对于RS签名算法,我们可以使用crypto/rsa包生成签名,并使用x509标准库验证签名。下面是示例代码:

  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "encoding/base64"
  8. "encoding/pem"
  9. "fmt"
  10. )
  11. func generateRSASignature(privateKey *rsa.PrivateKey, data []byte) ([]byte, error) {
  12. hashed := sha256.Sum256(data)
  13. return rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed[:])
  14. }
  15. func verifyRSASignature(publicKey *rsa.PublicKey, data []byte, signature []byte) bool {
  16. hashed := sha256.Sum256(data)
  17. err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed[:], signature)
  18. return err == nil
  19. }
  20. func main() {
  21. privatePEM := `
  22. -----BEGIN RSA PRIVATE KEY-----
  23. MIIEpAIBAAKCAQEAvhyMBkTq8V2LgDlH0iS+F5+5IzJmWivNQ6atRyU7vgBnpLsX
  24. f1SoC4sz7hDhZ4Pey3BQaOfevQwHcNNmFj6XpYVnU6qNTBV85Cv5GK0d0mXliYb9
  25. dPGDEc+yTD3XC78FJ/kwcrpSKqIFrMdTsl9eKtMUkizPnzS/WuErxagZqXh7IY04
  26. 9SPNz92sfDmQDD+pqWcaAE09fRsLiZdR8WXJl+CZRXY8xTfZcCJGkhYX2tq5C3hW
  27. 4jB/75i50aeKw5L8sQa9YBbno8XnSlxjOkD0Oo1mH+PRezFyUd7v6gKFZElBWwM+
  28. I3/xKbUVkFGRW+f2u34VqRkiNlTNUeVQTWHgqwIDAQABAoIBAQCNKsbzJwpE3kAq
  29. 90oc6nUUGAxV99t/s9AJvZJtS1rSaIme/Pp/gmdVRDWX/LArwkxBhTN28jlMrMAO
  30. COxZLGzkaxwbeZEp+8LVWtfnbl+yr0MPkkpljmFuCKlr/+ltEKTkLu/i3M10EVbj
  31. in/YZZe47Tk8vKddbhRL4OdQ/Cpcp4gDOhpFPhPC0nVKPLF05TVoejoTtBwSGARL
  32. dm/mSCs4OUYayfGibVGPHh7PrvnkmMtG/GVIaet6IVSJxz98IJHVsmRIbHKAB26M
  33. 5++rHYiKr2ypndtQDMXiZ/zgr15uxPB59RgTZHjTXzlDJlvfiTmVMYi/RYlkHNig
  34. dPFv9vjxAoGBAOwllGEld+shcvRM/qNgTjKgu6sfpoFPlwo30lnmqDpIZPW78+xP
  35. 44JPiykoyckSD+MVjia91ER43Wy14vfptzGX7qN2Qpb6dWJpKtY9h8xPZ4+siGkC
  36. 3L2Iws1wubVUvFzHb7C0yOc/aX+bBf5fRPcgTxzTQlnWgkZYu7bZjS/lAoGBAMmm
  37. nGMqwEowDp3sVqExTYlTrSpEVTgc6e1py4zyr7RmOwhL+oGtSENNrnWclxzO8A5i
  38. +p88XZZNzrONaPfi/Vrs50jQmM3vxdZVmTug0Xn/38rlMX06hIH/RBHx/g/HzYPA
  39. JU71H45VtFDIqMZQ57GRJbQLToNgY2xsaBe6mbqxAoGBAKO5DBA9geUMpwzvMsf/
  40. XL1cqENwYLmSPrcWyIaRXe/NhuHPMAvpukNBFTT5dqfJDAmpPPVOlKcVOfckqcTm
  41. yEwmavLc+QUhLPAn2NnPbyto+hNTKYy5J4CmaGJ9ijrTIrFZDGPTOsDdvl41ZeR6
  42. V37vHlmOk8Saq67/nVBfNoXJAoGBAJKhMH8xbtzIfFnWzsRwFJS65YehysUNmjyx
  43. dddl5n3NXB0T5ixfgz1TDg/D3CUfhcxCRFL10iBvm9gFCYjhCWhAXpLsnELFeRL0
  44. KkJ3twz1b0GKyCpKqP9cT4dHsUy2sXN9z7Phk9+OJ/PoVzz8vQrF5EKZl0kLmteS
  45. a26u6MjBAoGBAJTKR/7wUvNAGlzSPxIyZXrrS4ixRsP1tQfYKK3SueipRLURDnMF
  46. a8FZphmnoWLDhHNK4htWJaqUTA90itEiRXMMduVgvywqpB5cshwjQMye+Tt+2gNr
  47. IXYxzyFLvWhBaGT/+RRNM8OcgzOEEhiL/UtsKzjCMFdCXjwM/xscFDNG
  48. -----END RSA PRIVATE KEY-----
  49. `
  50. publicPEM := `
  51. -----BEGIN PUBLIC KEY-----
  52. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvhyMBkTq8V2LgDlH0iS+
  53. F5+5IzJmWivNQ6atRyU7vgBnpLsXf1SoC4sz7hDhZ4Pey3BQaOfevQwHcNNmFj6X
  54. pYVnU6qNTBV85Cv5GK0d0mXliYb9dPGDEc+yTD3XC78FJ/kwcrpSKqIFrMdTsl9e
  55. KtMUkizPnzS/WuErxagZqXh7IY049SPNz92sfDmQDD+pqWcaAE09fRsLiZdR8WXJ
  56. l+CZRXY8xTfZcCJGkhYX2tq5C3hW4jB/75i50aeKw5L8sQa9YBbno8XnSlxjOkD0
  57. Oo1mH+PRezFyUd7v6gKFZElBWwM+I3/xKbUVkFGRW+f2u34VqRkiNlTNUeVQTWHg
  58. qwIDAQAB
  59. -----END PUBLIC KEY-----
  60. `
  61. privateBlock, _ := pem.Decode([]byte(privatePEM))
  62. if privateBlock == nil {
  63. panic("failed to decode private key")
  64. }
  65. privateKey, err := x509.ParsePKCS1PrivateKey(privateBlock.Bytes)
  66. if err != nil {
  67. panic(err)
  68. }
  69. publicBlock, _ := pem.Decode([]byte(publicPEM))
  70. if publicBlock == nil {
  71. panic("failed to decode public key")
  72. }
  73. publicKeyInterface, err := x509.ParsePKIXPublicKey(publicBlock.Bytes)
  74. if err != nil {
  75. panic(err)
  76. }
  77. publicKey, ok := publicKeyInterface.(*rsa.PublicKey)
  78. if !ok {
  79. panic("public key is not of type *rsa.PublicKey")
  80. }
  81. data := []byte("hello world")
  82. // 签名数据
  83. signature, err := generateRSASignature(privateKey, data)
  84. if err != nil {
  85. panic(err)
  86. }
  87. fmt.Println(base64.StdEncoding.EncodeToString(signature))
  88. // 验证签名
  89. isValid := verifyRSASignature(publicKey, data, signature)
  90. fmt.Println(isValid)

在上面的代码中,我们使用了crypto/rsa包生成了一个SHA256哈希值的RSA签名,并将其编码为base64字符串输出。然后我们又定义了一个函数用于验证该签名是否有效。

  1. ES(ECDSA)签名与验证

对于ES签名算法,我们可以使用crypto/ecdsa包生成签名,并使用x509标准库验证签名。下面是示例代码:

”` package main

import (

  1. "crypto/ecdsa"
  2. "crypto/elliptic"
  3. "crypto/rand"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "encoding/pem"
  7. "fmt"

)

func generateECDSASignature(privateKey *ecdsa.PrivateKey, data []byte) ([]byte, error) {

  1. hashed := sha256.Sum256(data)
  2. return ecdsa.SignASN1(rand.Reader, privateKey, hashed[:])

}

func verifyECDSASignature(publicKey *ecdsa.PublicKey, data []byte, signature []byte) bool {

  1. hashed := sha256.Sum256(data)
  2. return ecdsa.VerifyASN1(publicKey, hashed[:], signature)

}

func main() {

  1. privatePEM := `

—–BEGIN EC PRIVATE KEY—– MHcCAQEEIPKQEZgH4u/rT6lQy7VxxTm2UyA+9Zwmp3M+bIq0V8BSoAoGCCqGSM49 AwEHoUQDQgAE5XbNzP5Otnp4oPGt/lJwCvLjpsnWd9FgrqiLkex7aRaeizHHT+9W EBmt4fOPZiAtzS6nI4eFYNOlgoYkkUG/hw== —–END EC PRIVATE KEY—– `

  1. publicPEM := `

—–BEGIN PUBLIC KEY—– MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE5XbNzP5Otnp4oPGt/lJwCvLjpsnW d9FgrqiLkex7aRaeizHHT+9WEBmt4fOPZiAtzS6nI4eFYNOlgoYkkUG/hw== —–END PUBLIC KEY—– `

  1. privateBlock, _ := pem.Decode([]byte(privatePEM))
  2. if privateBlock == nil {
  3. panic("failed to decode private key")
  4. }
  5. privateKey, err := x509.ParseECPrivateKey(privateBlock.Bytes)
  6. if err != nil {
  7. panic(err)
  8. }
  9. publicBlock, _ := pem.Decode([]byte(publicPEM))
  10. if publicBlock == nil {
  11. panic("failed to decode public key")
  12. }
  13. publicKeyInterface, err := x509.ParsePKIXPublicKey(publicBlock.Bytes)
  14. if err != nil {
  15. panic(err)
  16. }
  17. publicKey, ok

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=6560

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?