github.com/polarismesh/polaris@v1.17.8/common/rsa/rsa_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package rsa 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestGenerateRSAKey(t *testing.T) { 27 tests := []struct { 28 name string 29 err error 30 }{ 31 { 32 name: "generate rsa key", 33 err: nil, 34 }, 35 } 36 for _, tt := range tests { 37 t.Run(tt.name, func(t *testing.T) { 38 got, err := GenerateRSAKey() 39 t.Logf("PrivateKey: %s", got.PrivateKey) 40 t.Logf("PublicKey: %s", got.PublicKey) 41 assert.Nil(t, err) 42 }) 43 } 44 } 45 46 func TestEncryptToBase64(t *testing.T) { 47 type args struct { 48 plaintext []byte 49 } 50 tests := []struct { 51 name string 52 args args 53 want string 54 err error 55 }{ 56 { 57 name: "encrypt to base64", 58 args: args{ 59 plaintext: []byte("1234abcd!@#$"), 60 }, 61 }, 62 { 63 name: "encrypt lang text to base64", 64 args: args{ 65 plaintext: []byte(`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 66 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`), 67 }, 68 }, 69 } 70 for _, tt := range tests { 71 t.Run(tt.name, func(t *testing.T) { 72 rasKey, err := GenerateRSAKey() 73 assert.Nil(t, err) 74 ciphertext, err := EncryptToBase64(tt.args.plaintext, rasKey.PublicKey) 75 assert.Nil(t, err) 76 plaintext, err := DecryptFromBase64(ciphertext, rasKey.PrivateKey) 77 assert.Nil(t, err) 78 assert.Equal(t, plaintext, tt.args.plaintext) 79 }) 80 } 81 }