Skip to content

Commit e590dc7

Browse files
authored
Merge pull request #258 from jjhwan-h/feat/rsa-encrypt-helper
feat: add rsa encrypt helper
2 parents 57274b5 + b0cda64 commit e590dc7

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

dsl.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import (
1111
"crypto/hmac"
1212
"crypto/md5"
1313
"crypto/rand"
14+
"crypto/rsa"
1415
"crypto/sha1"
1516
"crypto/sha256"
1617
"crypto/sha512"
18+
"crypto/x509"
1719
"encoding/base64"
1820
"encoding/hex"
1921
"encoding/json"
22+
"encoding/pem"
2023
"errors"
2124
"fmt"
2225
"hash"
@@ -1457,6 +1460,45 @@ func init() {
14571460

14581461
return float64(mtime), nil
14591462
}))
1463+
1464+
MustAddFunction(NewWithPositionalArgs("rsa_encrypt",
1465+
2,
1466+
true,
1467+
func(args ...interface{}) (interface{}, error) {
1468+
if len(args) != 2 {
1469+
return nil, errors.New("rsa_encrypt expects 2 arguments: plaintext, pemPublicKey")
1470+
}
1471+
1472+
plaintext, ok1 := args[0].(string)
1473+
publicKeyPem, ok2 := args[1].(string)
1474+
1475+
if !ok1 || !ok2 {
1476+
return nil, errors.New("invalid arguments")
1477+
}
1478+
1479+
block, _ := pem.Decode([]byte(publicKeyPem))
1480+
if block == nil {
1481+
return nil, errors.New("invalid PEM format")
1482+
}
1483+
1484+
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
1485+
if err != nil {
1486+
return nil, err
1487+
}
1488+
1489+
rsaPub, ok := pub.(*rsa.PublicKey)
1490+
if !ok {
1491+
return nil, errors.New("not an RSA public key")
1492+
}
1493+
1494+
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPub, []byte(plaintext))
1495+
if err != nil {
1496+
return nil, fmt.Errorf("RSA encryption failed: %w", err)
1497+
}
1498+
return base64.StdEncoding.EncodeToString(ciphertext), nil
1499+
}),
1500+
)
1501+
14601502
DefaultHelperFunctions = HelperFunctions()
14611503
FunctionNames = GetFunctionNames(DefaultHelperFunctions)
14621504
}

dsl_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
297297
replace(arg1, arg2, arg3 interface{}) interface{}
298298
replace_regex(arg1, arg2, arg3 interface{}) interface{}
299299
reverse(arg1 interface{}) interface{}
300+
rsa_encrypt(arg1, arg2 interface{}) interface{}
300301
sha1(arg1 interface{}) interface{}
301302
sha256(arg1 interface{}) interface{}
302303
sha512(arg1 interface{}) interface{}
@@ -454,6 +455,15 @@ func TestDslExpressions(t *testing.T) {
454455
`zlib_decode(hex_decode("789cf248cdc9c907040000ffff058c01f5"), 100)`: "Hello",
455456
`gzip_decode(hex_decode("1f8b08000000000000fff248cdc9c907040000ffff8289d1f705000000"), 100)`: "Hello",
456457
`inflate(hex_decode("f248cdc9c907040000ffff"), 100)`: "Hello",
458+
`rsa_encrypt("plaindata", "-----BEGIN PUBLIC KEY-----
459+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtKqKDIZyXltCyLVym+VL
460+
N4kMQHoazrJ7G5GbOSITuFaV0lpbXTw9VmW8wkyxG0U9b5zMaIfWyF5T9DWw/AcI
461+
9ehszNYTy1U6KgNN94bZzILsWnQ3M7o8T9qZxITNBd/90VpW2O0ClR1z+gB4ls1C
462+
cSy4ym0pQ7ZKMEJbWYxFuw3CJfWAFbdXcULgqIG0K7Nh++g6v5XLRceqxOW9j9Mc
463+
29THVYk8uvF8gEOZBvM4RnhJhJX03ACRCHqBg4CdKaYaWIWc+eOxZrBg0iAfWpy+
464+
vOZml6PnbXH+Z1+yVskAoyGKnOxRSaD0DJY6xq1x3z5AoVImLsCLSkJr2D+4W+EC
465+
PQIDAQAB
466+
-----END PUBLIC KEY-----") != ""`: true,
457467
}
458468

459469
testDslExpressions(t, dslExpressions)

0 commit comments

Comments
 (0)