@@ -10,7 +10,7 @@ You may obtain a copy of the License at
10
10
Unless required by applicable law or agreed to in writing, software
11
11
distributed under the License is distributed on an "AS IS" BASIS,
12
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
13
+ See the License for the specific language 24 permissions and
14
14
limitations under the License.
15
15
*/
16
16
package render
@@ -19,16 +19,22 @@ import (
19
19
"bytes"
20
20
"context"
21
21
"crypto/md5"
22
+ "crypto/rand"
22
23
"crypto/sha256"
23
24
"encoding/base64"
24
25
"encoding/hex"
25
26
"encoding/json"
26
27
"fmt"
27
28
"html/template"
28
29
"io"
29
- "math/rand"
30
+ mathrand "math/rand"
30
31
"strings"
31
32
33
+ "crypto/rsa"
34
+ "crypto/x509"
35
+ "encoding/pem"
36
+ "errors"
37
+
32
38
"github.com/Masterminds/sprig/v3"
33
39
"github.com/linuxsuren/api-testing/pkg/secret"
34
40
"github.com/linuxsuren/api-testing/pkg/util"
@@ -85,6 +91,7 @@ func FuncMap() template.FuncMap {
85
91
}
86
92
funcs [item .FuncName ] = item .Func
87
93
}
94
+ funcs ["rasEncryptWithPublicKey" ] = rasEncryptWithPublicKey
88
95
return funcs
89
96
}
90
97
@@ -158,7 +165,7 @@ var advancedFuncs = []AdvancedFunc{{
158
165
}, {
159
166
FuncName : "randEnum" ,
160
167
Func : func (items ... string ) string {
161
- return items [rand .Intn (len (items ))]
168
+ return items [mathrand .Intn (len (items ))]
162
169
},
163
170
}, {
164
171
FuncName : "randEmail" ,
@@ -217,3 +224,28 @@ type AdvancedFunc struct {
217
224
GoDogExper string
218
225
Generator func (ctx context.Context , fields string ) (err error )
219
226
}
227
+
228
+ // rasEncryptWithPublicKey encrypts the given content with the provided public key
229
+ func rasEncryptWithPublicKey (content , key string ) (string , error ) {
230
+ block , _ := pem .Decode ([]byte (key ))
231
+ if block == nil {
232
+ return "" , errors .New ("failed to parse PEM block containing the public key" )
233
+ }
234
+
235
+ pub , err := x509 .ParsePKIXPublicKey (block .Bytes )
236
+ if err != nil {
237
+ return "" , fmt .Errorf ("failed to parse DER encoded public key: %s" , err )
238
+ }
239
+
240
+ rsaPub , ok := pub .(* rsa.PublicKey )
241
+ if ! ok {
242
+ return "" , errors .New ("key type is not RSA" )
243
+ }
244
+
245
+ encryptedData , err := rsa .EncryptPKCS1v15 (rand .Reader , rsaPub , []byte (content ))
246
+ if err != nil {
247
+ return "" , fmt .Errorf ("failed to encrypt with RSA public key: %s" , err )
248
+ }
249
+
250
+ return base64 .StdEncoding .EncodeToString (encryptedData ), nil
251
+ }
0 commit comments