github.com/godaddy-x/freego@v1.0.156/rsa_test.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/rsa"
     6  	"crypto/x509"
     7  	"encoding/base64"
     8  	"encoding/pem"
     9  	"fmt"
    10  	"github.com/godaddy-x/freego/utils/crypto"
    11  	"testing"
    12  )
    13  
    14  const keyfile = "testrsa1.key"
    15  const pemfile = "testrsa1.pem"
    16  const testmsg = "ABCDEFGdsfdfgfg中阿斯蒂芬阿斯顿发的方式噶地方官!#@!~"
    17  
    18  const prikeybase64 = "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlDWEFJQkFBS0JnUUMyY05IUHlqSDdPUFM2WjJIcHQ2YmZ5UEFKejZ3aXh5Ykxxbm5EVWU5NVBWd2FHdTFWClhLc0EyOU95L1R4ZG1BbmRaSkFySFJLYlNnNURvTEVQY3BYVEpGSitFaGJtME1ZeWV2THVZYzVSWGQ3TkltbUYKWEpSMFlZcHZPZmhEZE5FbXNWS3R4UXVoazBsREhmOWJvREFKTWVPb2hwdlBWanBnQmVZQW9aK1VCd0lEQVFBQgpBb0dBZjZ0M3gvZHcvcU1lNzRzRlErN1hBbWUxUXNoblozY0NPU2cxU1cvL0sxSzdMekdFd0dXMjdVVG9ZcXRBCklTY1NVREhkaWE0d3BTY3YwRGVWY0gvNVE3Y3A0U2t0RFRUNG1CWE5haGtpL3I5UDExSXI4MnEvemVDRXRTb2oKN0xFNWpVSG1jVXdvZ1B3MEdTRUhiNVQ0ZlJlV1M0QlpBUm5JWkY3M0xTVlVnNEVDUVFESW1nSTJNc1V1NU1QWQpmS2V4S0hIWjlISDdleHliaUt1ZkN3VjNaWHlldmJIRU9UcTJHSnpMbjMxK0l6VTlWc3hwU1VqL0RSeko3NFd4Cmo5LzdpWExuQWtFQTZOTGllNmdxOUQvSXBhczZLVGt4WThzbVBDaHhDNE9HWmtxa25JM1pTS1pub2d2SE1wTVIKeXdvdjl6bkhSR2QzSzZjZHhUbGY2SUpleFV1cWs0WFI0UUpBWTRXVXgxTFU1UGoxK1BlUE1xTkFLTVBQc05aWgpVUWl6TElxSlFiMEY0TE4zK0VQMFR0ZFRJdXFUbGZyZHRQclZHdjhTeWdhMVc3SUxnQlpESjBYL3pRSkJBTnJUCm90VXdxVGFxWUk3OWtZdS9XcUYrQmZEUzNmVkJhR2ZxVGk5cXowZU9SNmN4eE1iUEhoRWxBUkl2dHcrZTQ0NGUKNDBkRWR0VlUrM2dhZHpkeXRtRUNRRjF5ODNZWnkzdm9RNGtCNjFDcjJLUGxLQzJuenptYmFqVmRodWNZNVRnYwpUcVJEQjh4WXNXOHdjQ1Biek51em50cXYraEhnYkhsc3grMUpBNGxrS1lrPQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo="
    19  const pubkeybase64 = "LS0tLS1CRUdJTiBSU0EgUFVCTElDSyBLRVktLS0tLQpNSUdKQW9HQkFMWncwYy9LTWZzNDlMcG5ZZW0zcHQvSThBblByQ0xISnN1cWVjTlI3M2s5WEJvYTdWVmNxd0RiCjA3TDlQRjJZQ2Qxa2tDc2RFcHRLRGtPZ3NROXlsZE1rVW40U0Z1YlF4ako2OHU1aHpsRmQzczBpYVlWY2xIUmgKaW04NStFTjAwU2F4VXEzRkM2R1RTVU1kLzF1Z01Ba3g0NmlHbTg5V09tQUY1Z0NobjVRSEFnTUJBQUU9Ci0tLS0tRU5EIFJTQSBQVUJMSUNLIEtFWS0tLS0tCg=="
    20  
    21  //func getobj() *crypto.RsaObj {
    22  //	obj := &crypto.RsaObj{}
    23  //	if err := obj.LoadRsaFile(keyfile); err != nil {
    24  //		panic(err)
    25  //	}
    26  //	return obj
    27  //}
    28  //
    29  //var (
    30  //	obj = getobj()
    31  //)
    32  
    33  func BenchmarkRSA(b *testing.B) {
    34  	b.StopTimer()
    35  	b.StartTimer()
    36  	for i := 0; i < b.N; i++ { //use b.N for looping
    37  		obj := &crypto.RsaObj{}
    38  		if err := obj.LoadRsaKeyFileBase64(prikeybase64); err != nil {
    39  			panic(err)
    40  		}
    41  		res, err := obj.Encrypt([]byte(testmsg))
    42  		if err != nil {
    43  			panic(err)
    44  		}
    45  		//fmt.Println("RSA公钥加密结果: ", res)
    46  		_, err = obj.Decrypt(res)
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  		//fmt.Println("RSA私钥解密结果: ", string(res2))
    51  	}
    52  }
    53  
    54  func TestRsaCreateFile(t *testing.T) {
    55  	obj := &crypto.RsaObj{}
    56  	obj.CreateRsaFile(keyfile, pemfile)
    57  }
    58  
    59  func TestRsaCreateFileBase64(t *testing.T) {
    60  	obj := &crypto.RsaObj{}
    61  	if err := obj.CreateRsaFileBase64(1024); err != nil {
    62  		panic(err)
    63  	}
    64  	fmt.Println(obj.GetPrivateKey())
    65  	fmt.Println(obj.GetPublicKey())
    66  
    67  	obj1 := &crypto.RsaObj{}
    68  	if err := obj1.LoadRsaPemFileBase64(obj.PublicKeyBase64); err != nil {
    69  		panic(err)
    70  	}
    71  }
    72  
    73  func TestRsaLoadFile1(t *testing.T) {
    74  	var (
    75  		publicKey       *rsa.PublicKey
    76  		publicKeyString string
    77  	)
    78  
    79  	if pri, err := rsa.GenerateKey(rand.Reader, 1024); err != nil {
    80  		panic(err)
    81  	} else {
    82  		publicKey = &pri.PublicKey
    83  		p := x509.MarshalPKCS1PrivateKey(pri)
    84  		fmt.Println("私钥: ")
    85  		fmt.Println(string(p))
    86  	}
    87  	// 将publicKey转换为PKIX, ASN.1 DER格式
    88  	if derPkix, err := x509.MarshalPKIXPublicKey(publicKey); err != nil {
    89  		panic(err)
    90  	} else {
    91  		// 设置PEM编码结构
    92  		block := pem.Block{
    93  			Type:  "RSA PUBLIC KEY",
    94  			Bytes: derPkix,
    95  		}
    96  		// 将publicKey以字符串形式返回给javascript
    97  		publicKeyString = string(pem.EncodeToMemory(&block))
    98  		fmt.Println("公钥")
    99  		fmt.Println(publicKeyString)
   100  	}
   101  }
   102  
   103  func TestRsaLoadFile(t *testing.T) {
   104  	obj := &crypto.RsaObj{}
   105  	if err := obj.LoadRsaFile(keyfile); err != nil {
   106  		panic(err)
   107  	}
   108  	//obj.CreateRsaPubFile("test.pem")
   109  	obj2 := &crypto.RsaObj{}
   110  	if err := obj2.LoadRsaPemFile("testrsa1.pem"); err != nil {
   111  		panic(err)
   112  	}
   113  	//a, _ := obj2.Encrypt([]byte("123465"))
   114  	a := "lQhuSr4Ocj0537i338VEgl8g2iI0EsnSWXIJYuF4EsgMLPmOeFT0mzw4fNY1JN2lo++b/AA5wld4WAConPQV5jQewBevdcw9du4y6pbDC98ZEgUQkqhnCuZPjO06VVaBQpxN01NUPVpdBW3j6wnG885h+S1cbIbAGlu07vvSrlsY7kFN/nCLQoki+MaBdOZ0UcnR/wgyNG+bucTlcpywwn3L0gIWAQfHIBx/LFEAUHe0afUqEallZNoLG/yEk6Jqnz0PtpsR07PfMK7FcjWePHnswNa07F9RiKKTKjJO5QK+oKFW5gvyA8DjYemcFciGwu+NIGHsTFIgVU86Tx7cFw=="
   115  	b, err := obj.Decrypt(a)
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	fmt.Println("Go解密结果: ", string(b))
   120  }
   121  
   122  func TestRsaPubkeyEncrypt(t *testing.T) {
   123  	obj := &crypto.RsaObj{}
   124  	if err := obj.LoadRsaFile(keyfile); err != nil {
   125  		panic(err)
   126  	}
   127  	res, err := obj.Encrypt([]byte(testmsg))
   128  	if err != nil {
   129  		panic(err)
   130  	}
   131  	fmt.Println("RSA公钥加密结果: ", res)
   132  }
   133  
   134  func TestRsaPrikeyDecrypt(t *testing.T) {
   135  	obj := &crypto.RsaObj{}
   136  	if err := obj.LoadRsaFile(keyfile); err != nil {
   137  		panic(err)
   138  	}
   139  	res, err := obj.Encrypt([]byte(testmsg))
   140  	if err != nil {
   141  		panic(err)
   142  	}
   143  	fmt.Println("RSA公钥加密结果: ", res)
   144  	res2, err := obj.Decrypt(res)
   145  	if err != nil {
   146  		panic(err)
   147  	}
   148  	fmt.Println("RSA私钥解密结果: ", string(res2))
   149  }
   150  
   151  func TestRsaSignAndVerify(t *testing.T) {
   152  	obj := &crypto.RsaObj{}
   153  	if err := obj.LoadRsaFile(keyfile); err != nil {
   154  		panic(err)
   155  	}
   156  	res, err := obj.Sign([]byte(testmsg))
   157  	if err != nil {
   158  		panic(err)
   159  	}
   160  	fmt.Println("RSA私钥签名结果: ", base64.StdEncoding.EncodeToString(res))
   161  	if err := obj.Verify([]byte(testmsg), res); err == nil {
   162  		fmt.Println("RSA公钥验签结果: ", err == nil)
   163  	} else {
   164  		panic(err)
   165  	}
   166  }