scrm-go

usersig_test.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package tencentsig
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/assert"
  5. "log"
  6. "os"
  7. "strings"
  8. "testing"
  9. )
  10. var (
  11. sdkAppId = "1234567890"
  12. identifier = "zhangshan"
  13. l = log.New(os.Stdout, "[tencentsig - test] ", log.LstdFlags)
  14. )
  15. func doVerifyUserSig(t *testing.T, pemPrivateKey, pemPublicKey string) {
  16. conf := NewConf(sdkAppId, identifier).WithExpire(defaultExpire)
  17. userSig, err := conf.GenUserSig(pemPrivateKey)
  18. assert.Nil(t, err)
  19. l.Print("userSig:", userSig)
  20. c, valid, err := VerifyUserSig(pemPublicKey, userSig)
  21. assert.Nil(t, err)
  22. assert.True(t, valid)
  23. b, _ := json.MarshalIndent(c, "", " ")
  24. l.Print("conf:", string(b))
  25. }
  26. // 准备
  27. /*
  28. # list curves
  29. openssl ecparam -list_curves
  30. # prime256v1 curve
  31. openssl ecparam -name prime256v1 -genkey -noout -out private.pem
  32. openssl pkcs8 -topk8 -nocrypt -in private.pem -out private-pkcs8.pem && cat private-pkcs8.pem
  33. openssl ec -in private.pem -pubout
  34. # secp256k1 curve
  35. openssl ecparam -name secp256k1 -genkey -noout -out private.pem
  36. openssl pkcs8 -topk8 -nocrypt -in private.pem -out private-pkcs8.pem && cat private-pkcs8.pem
  37. openssl ec -in private.pem -pubout
  38. */
  39. func TestVerifyUserSig(t *testing.T) {
  40. // prime256v1
  41. doVerifyUserSig(
  42. t,
  43. strings.TrimSpace(`
  44. -----BEGIN PRIVATE KEY-----
  45. MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgJzqd3dF+O6vd+bGJ
  46. 7tGA7TLsWNzbYBKRGELEA65ywQahRANCAATIBFu6F5SlqrPFkuhi46IRXXKyEiuU
  47. g8pP+n3L5ZSiW3o0N58P0Ix77PrRVSXLfHd5VqeyF2CWWDUQZyA/butY
  48. -----END PRIVATE KEY-----
  49. `),
  50. strings.TrimSpace(`
  51. -----BEGIN PUBLIC KEY-----
  52. MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyARbuheUpaqzxZLoYuOiEV1yshIr
  53. lIPKT/p9y+WUolt6NDefD9CMe+z60VUly3x3eVanshdgllg1EGcgP27rWA==
  54. -----END PUBLIC KEY-----
  55. `),
  56. )
  57. // secp256k1
  58. doVerifyUserSig(
  59. t,
  60. strings.TrimSpace(`
  61. -----BEGIN PRIVATE KEY-----
  62. MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgkRrBHsxAXy4ssvSYsJIM
  63. TUzzLIHOeUQ/QKygM3JhvDahRANCAATyucyxciWHFclVxRPW7zJ6d51F5au6xnZk
  64. bjkiDOpa6gl8JhdeWcKLYgRb5raHNq/JYUYJSrsH29whxdx0lpq7
  65. -----END PRIVATE KEY-----
  66. `),
  67. strings.TrimSpace(`
  68. -----BEGIN PUBLIC KEY-----
  69. MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE8rnMsXIlhxXJVcUT1u8yenedReWrusZ2
  70. ZG45IgzqWuoJfCYXXlnCi2IEW+a2hzavyWFGCUq7B9vcIcXcdJaauw==
  71. -----END PUBLIC KEY-----
  72. `),
  73. )
  74. }