rsa_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package rsa
  2. import (
  3. "testing"
  4. )
  5. const (
  6. publicKey = `-----BEGIN PUBLIC KEY-----
  7. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1O3p0JN0/RrP7eY3f81i
  8. zPf16FS0WMNGCJkd+y5c6yBzUvN0IEeoxiIWIBhoMKH0pzlzBg0rfttojSodOgNo
  9. m/UCAzAYEgdIsNee5LSN/7e0T2/QvsIAHINuA8gI8fGoGiSA2TEzpUo6aVXwhZT3
  10. 4GGRdrSJ+m4iVk/Kt95tavBNk+NDVSeb5xAjxBchT5BjAMMlE0ffGZb0MMjjO5+e
  11. 9Tn8f99M2VMqpzXHXZzv1ABmqufzS20iWcSvnjhWcJ9hiKwO8Z30GgJyACmml+HM
  12. xLYEFN9h2MWYgxLm9Z0rLMrWwMM+E2rCs8tsxAD5sO9RZMJPl1C0FIsMR53ngqbz
  13. owIDAQAB
  14. -----END PUBLIC KEY-----`
  15. privateKey = `-----BEGIN RSA PRIVATE KEY-----
  16. MIIEpgIBAAKCAQEA1O3p0JN0/RrP7eY3f81izPf16FS0WMNGCJkd+y5c6yBzUvN0
  17. IEeoxiIWIBhoMKH0pzlzBg0rfttojSodOgNom/UCAzAYEgdIsNee5LSN/7e0T2/Q
  18. vsIAHINuA8gI8fGoGiSA2TEzpUo6aVXwhZT34GGRdrSJ+m4iVk/Kt95tavBNk+ND
  19. VSeb5xAjxBchT5BjAMMlE0ffGZb0MMjjO5+e9Tn8f99M2VMqpzXHXZzv1ABmqufz
  20. S20iWcSvnjhWcJ9hiKwO8Z30GgJyACmml+HMxLYEFN9h2MWYgxLm9Z0rLMrWwMM+
  21. E2rCs8tsxAD5sO9RZMJPl1C0FIsMR53ngqbzowIDAQABAoIBAQCO1RE1ItUlO6kj
  22. Un0ENAgEqojAUqGvsT33Yo7kAZO+/cOeb0UEqk0iq5b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Jk3ELB/FHY8xzZ4jF1wG/a1CK681Xm6SuU5KIELDSAUNoou6OPG
  39. mS8gU20MMPAeV2z7khyDcSxlHsUyL73eLeaakbQov9NMW7cc99XX4wnP4W7FRpmr
  40. QbHmKuHIRFHCFv+XX8c0aK2mDZMUlzJdy4FgD/YCEZ7kZMZKyvZW/ZuV
  41. -----END RSA PRIVATE KEY-----`
  42. )
  43. func TestEncrypt(t *testing.T) {
  44. str, err := NewPublic(publicKey).Encrypt("123456")
  45. if err != nil {
  46. t.Error("rsa public encrypt error", err)
  47. return
  48. }
  49. t.Log(str)
  50. }
  51. func TestDecrypt(t *testing.T) {
  52. decryptStr := "KTKXckjkCLI6Vk_y_XROnY-a6nJpllruL-CX-v_2AFxfghA2tZ2nkQyS6d1-IIYMlgwm4ivwlzy-phLtaN9BB03htA5D9rwjA_JwYtqAG4iwuvgaDl2SiZ_H2ACv-aV1kNRgnyjh14hs0JiSt5VHEiJ3D2xYzOCKwtEzoo0WczJ-MYb3u_-bfcnm9YtvgtG5-y3Jy7WYr-IwXdBKqPO0E-jzrtY7m3Q1yC4znHdzjNpxCj0I6YRx4CZ362b706qNX7sl3E5KTJeSmYrsurB-SxQT1CaqGzVt7mshx1v2qGnv5NBNXpj7ZPKWGJbgaCUxcuxd1Mg0o81HnfbsGuSlFQ=="
  53. str, err := NewPrivate(privateKey).Decrypt(decryptStr)
  54. if err != nil {
  55. t.Error("rsa private decrypt error", err)
  56. return
  57. }
  58. t.Log(str)
  59. }
  60. func BenchmarkEncryptAndDecrypt(b *testing.B) {
  61. b.ResetTimer()
  62. rsaPublic := NewPublic(publicKey)
  63. rsaPrivate := NewPrivate(privateKey)
  64. for i := 0; i < b.N; i++ {
  65. encryptString, _ := rsaPublic.Encrypt("123456")
  66. rsaPrivate.Decrypt(encryptString)
  67. }
  68. }