1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package integration
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"testing"
"gotest.tools/v3/fs"
)
func genTestKeys() (string, string) {
rsaPriv, _ := rsa.GenerateKey(rand.Reader, 4096)
rsaPub := rsaPriv.PublicKey
privBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(rsaPriv),
}
testPrivKey := string(pem.EncodeToMemory(privBlock))
b, _ := x509.MarshalPKIXPublicKey(&rsaPub)
pubBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: b,
}
testPubKey := string(pem.EncodeToMemory(pubBlock))
return testPrivKey, testPubKey
}
func setupCryptoTest(t *testing.T) *fs.Dir {
testPrivKey, testPubKey := genTestKeys()
tmpDir := fs.NewDir(t, "gomplate-inttests",
fs.WithFile("testPrivKey", testPrivKey),
fs.WithFile("testPubKey", testPubKey),
)
t.Cleanup(tmpDir.Remove)
return tmpDir
}
func TestCrypto_RSACrypt(t *testing.T) {
tmpDir := setupCryptoTest(t)
o, e, err := cmd(t,
"--experimental",
"-i", `{{ crypto.RSAGenerateKey 2048 -}}`,
"-o", `key.pem`).
withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "")
o, e, err = cmd(t,
"--experimental",
"-c", "privKey=./key.pem?type=text/plain",
"-i", `{{ $pub := crypto.RSADerivePublicKey .privKey -}}
{{ $enc := "hello" | crypto.RSAEncrypt $pub -}}
{{ crypto.RSADecryptBytes .privKey $enc | conv.ToString }}
{{ crypto.RSADecrypt .privKey $enc }}
`).
withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "hello\nhello\n")
}
|