Add RSA encryption function to templates (#502)
* Add RSA encryption function to templates * fix the compile errors --------- Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
2e50d1c4ea
commit
3aceced901
|
@ -10,7 +10,7 @@ You may obtain a copy of the License at
|
||||||
Unless required by applicable law or agreed to in writing, software
|
Unless required by applicable law or agreed to in writing, software
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
See the License for the specific language governing permissions and
|
See the License for the specific language 24 permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
package render
|
package render
|
||||||
|
@ -19,6 +19,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
@ -26,9 +27,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
mathrand "math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/Masterminds/sprig/v3"
|
"github.com/Masterminds/sprig/v3"
|
||||||
"github.com/linuxsuren/api-testing/pkg/secret"
|
"github.com/linuxsuren/api-testing/pkg/secret"
|
||||||
"github.com/linuxsuren/api-testing/pkg/util"
|
"github.com/linuxsuren/api-testing/pkg/util"
|
||||||
|
@ -85,6 +91,7 @@ func FuncMap() template.FuncMap {
|
||||||
}
|
}
|
||||||
funcs[item.FuncName] = item.Func
|
funcs[item.FuncName] = item.Func
|
||||||
}
|
}
|
||||||
|
funcs["rasEncryptWithPublicKey"] = rasEncryptWithPublicKey
|
||||||
return funcs
|
return funcs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +165,7 @@ var advancedFuncs = []AdvancedFunc{{
|
||||||
}, {
|
}, {
|
||||||
FuncName: "randEnum",
|
FuncName: "randEnum",
|
||||||
Func: func(items ...string) string {
|
Func: func(items ...string) string {
|
||||||
return items[rand.Intn(len(items))]
|
return items[mathrand.Intn(len(items))]
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
FuncName: "randEmail",
|
FuncName: "randEmail",
|
||||||
|
@ -217,3 +224,28 @@ type AdvancedFunc struct {
|
||||||
GoDogExper string
|
GoDogExper string
|
||||||
Generator func(ctx context.Context, fields string) (err error)
|
Generator func(ctx context.Context, fields string) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rasEncryptWithPublicKey encrypts the given content with the provided public key
|
||||||
|
func rasEncryptWithPublicKey(content, key string) (string, error) {
|
||||||
|
block, _ := pem.Decode([]byte(key))
|
||||||
|
if block == nil {
|
||||||
|
return "", errors.New("failed to parse PEM block containing the public key")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to parse DER encoded public key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rsaPub, ok := pub.(*rsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("key type is not RSA")
|
||||||
|
}
|
||||||
|
|
||||||
|
encryptedData, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPub, []byte(content))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to encrypt with RSA public key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return base64.StdEncoding.EncodeToString(encryptedData), nil
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,11 @@ package render
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/pem"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -233,3 +238,45 @@ func TestSecret(t *testing.T) {
|
||||||
assert.Equal(t, "hello", string(data))
|
assert.Equal(t, "hello", string(data))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRasEncryptWithPublicKey(t *testing.T) {
|
||||||
|
// Generate a new RSA key pair
|
||||||
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to generate private key: %v", err)
|
||||||
|
}
|
||||||
|
publicKey := &privateKey.PublicKey
|
||||||
|
|
||||||
|
// Encode the public key to PEM format
|
||||||
|
pubASN1, err := x509.MarshalPKIXPublicKey(publicKey)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to marshal public key: %v", err)
|
||||||
|
}
|
||||||
|
pubBytes := pem.EncodeToMemory(&pem.Block{
|
||||||
|
Type: "RSA PUBLIC KEY",
|
||||||
|
Bytes: pubASN1,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Encrypt a message using the public key
|
||||||
|
message := "hello world"
|
||||||
|
encryptedMessage, err := rasEncryptWithPublicKey(message, string(pubBytes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to encrypt message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt the message using the private key
|
||||||
|
decodedMessage, err := base64.StdEncoding.DecodeString(encryptedMessage)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to decode message: %v", err)
|
||||||
|
}
|
||||||
|
decryptedBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, decodedMessage)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to decrypt message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the decrypted message
|
||||||
|
decryptedMessage := string(decryptedBytes)
|
||||||
|
if decryptedMessage != message {
|
||||||
|
t.Fatalf("Decrypted message does not match original. Got: %s, want: %s", decryptedMessage, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue