crypto functions
A set of crypto-related functions to be able to perform hashing and (simple!)
encryption operations with gomplate.
Note: These functions are mostly wrappers of existing functions in the Go standard library. The authors of gomplate are not cryptographic experts, however, and so can not guarantee correctness of implementation. It is recommended to have your resident security experts inspect gomplate’s code before using gomplate for critical security infrastructure!
crypto.Bcrypt
Uses the bcrypt password hashing algorithm to generate the hash of a given string. Wraps the golang.org/x/crypto/brypt package.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
cost |
(optional) the cost, as a number from 4 to 31 - defaults to 10 |
input |
(required) the input to hash, usually a password |
Examples
crypto.YescryptMCF(unreleased) (experimental)
Unreleased: This function is in development, and not yet available in released builds of gomplate.
Experimental: This function is experimental and may be enabled with the --experimental flag.
Uses the Yescrypt password hashing algorithm to generate the hash of a given string.
Wraps the github.com/openwall/yescrypt-go package.
Yescrypt is a modern, memory-hard key derivation function designed as an extension of scrypt, providing resistance to GPU/ASIC attacks.
Usage
Arguments
| name | description |
|---|---|
cost |
(optional) the cost parameter (log₂ of the iteration count) - integer from 10 to 18 - defaults to 14 |
blockSize |
(optional) the block size parameter (r) - integer from 1 to 32 - defaults to 8 |
salt |
(optional) the salt string used in hashing - defaults to a random alphanumeric string of length 16 |
input |
(required) the input to hash, usually a password |
Examples
crypto.Yescrypt(unreleased) (experimental)
Unreleased: This function is in development, and not yet available in released builds of gomplate.
Experimental: This function is experimental and may be enabled with the --experimental flag.
Uses the Yescrypt password hashing algorithm to generate the hash of a given string.
Wraps the github.com/openwall/yescrypt-go package.
Yescrypt is a modern, memory-hard key derivation function designed as an extension of scrypt, providing resistance to GPU/ASIC attacks.
This function outputs the binary result as a hexadecimal string.
Usage
Arguments
| name | description |
|---|---|
input |
(required) the input to hash, usually a password |
salt |
(required) the salt string used in hashing |
cost |
(required) the cost parameter, integer > 1 and a power of 2 |
blockSize |
(required) the block size parameter (r) - integer > 0 |
keyLen |
(required) desired length of derived key |
Examples
crypto.AESDecrypt
Alias: crypto.DecryptAES (deprecated)
Decrypts the given input using the given key. By default, uses AES-256-CBC, but supports 128- and 192-bit keys as well.
This function prints the output as a string. Note that this may result in
unreadable text if the decrypted payload is binary. See
crypto.AESDecryptBytes for another method.
This function is suitable for decrypting data that was encrypted by
Helm’s encryptAES function, when the input is base64-decoded, and when
using 256-bit keys.
Added in gomplate v3.11.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the key to use for decryption |
keyBits |
(optional) the key length to use - defaults to 256 |
input |
(required) the input to decrypt |
Examples
crypto.AESDecryptBytes
Alias: crypto.DecryptAESBytes (deprecated)
Decrypts the given input using the given key. By default, uses AES-256-CBC, but supports 128- and 192-bit keys as well.
This function outputs the raw byte array, which may be sent as input to other functions.
This function is suitable for decrypting data that was encrypted by
Helm’s encryptAES function, when the input is base64-decoded, and when
using 256-bit keys.
Added in gomplate v3.11.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the key to use for decryption |
keyBits |
(optional) the key length to use - defaults to 256 |
input |
(required) the input to decrypt |
Examples
crypto.AESEncrypt
Alias: crypto.EncryptAES (deprecated)
Encrypts the given input using the given key. By default, uses AES-256-CBC, but supports 128- and 192-bit keys as well.
This function is suitable for encrypting data that will be decrypted by
Helm’s decryptAES function, when the output is base64-encoded, and when
using 256-bit keys.
Added in gomplate v3.11.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the key to use for encryption |
keyBits |
(optional) the key length to use - defaults to 256 |
input |
(required) the input to encrypt |
Examples
crypto.DerivePublicKey
Derive a public key from any supported private key type (RSA, ECDSA, or Ed25519) and output in PKIX ASN.1 DER form. The key type is auto-detected from the PEM block type.
This is a unified function that can replace the algorithm-specific
crypto.RSADerivePublicKey, crypto.ECDSADerivePublicKey, and
crypto.Ed25519DerivePublicKey functions.
Added in gomplate v5.1.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the private key to derive a public key from |
Examples
crypto.ECDSAGenerateKey
Generate a new Elliptic Curve Private Key and output in PEM-encoded PKCS#1 ASN.1 DER form.
Go’s standard NIST P-224, P-256, P-384, and P-521 elliptic curves are all supported.
Default curve is P-256 and can be overridden with the optional curve
parameter.
Added in gomplate v3.11.0
Usage
Arguments
| name | description |
|---|---|
curve |
(optional) One of Go’s standard NIST curves, P-224, P-256, P-384, or P-521 - |
| defaults to P-256. | |
Examples
crypto.ECDSADerivePublicKey
Derive a public key from an elliptic curve private key and output in PKIX ASN.1 DER form.
Added in gomplate v3.11.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the private key to derive a public key from |
Examples
crypto.Ed25519GenerateKey
Generate a new Ed25519 Private Key and output in PEM-encoded PKCS#8 ASN.1 DER form.
Added in gomplate v4.0.0
Usage
Examples
crypto.Ed25519GenerateKeyFromSeed
Generate a new Ed25519 Private Key from a random seed and output in PEM-encoded PKCS#8 ASN.1 DER form.
Added in gomplate v4.0.0
Usage
Arguments
| name | description |
|---|---|
encoding |
(required) the encoding that the seed is in (hex or base64) |
seed |
(required) the random seed encoded in either base64 or hex |
Examples
crypto.Ed25519DerivePublicKey
Derive a public key from an Ed25519 private key and output in PKIX ASN.1 DER form.
Added in gomplate v4.0.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the private key to derive a public key from |
Examples
crypto.PBKDF2
Run the Password-Based Key Derivation Function #2 as defined in RFC 8018 (PKCS #5 v2.1).
This function outputs the binary result as a hexadecimal string.
Added in gomplate v2.3.0
Usage
Arguments
| name | description |
|---|---|
password |
(required) the password to use to derive the key |
salt |
(required) the salt |
iter |
(required) iteration count |
keylen |
(required) desired length of derived key |
hashfunc |
(optional) the hash function to use - must be one of the allowed functions (either in the SHA-1 or SHA-2 sets). Defaults to SHA-1 |
Examples
crypto.PBKDF2MCF(unreleased) (experimental)
Unreleased: This function is in development, and not yet available in released builds of gomplate.
Experimental: This function is experimental and may be enabled with the --experimental flag.
Run the Password-Based Key Derivation Function #2 as defined in RFC 8018 (PKCS #5 v2.1).
This function outputs a Modular Crypt Format (MCF) result string.
Usage
Arguments
| name | description |
|---|---|
password |
(required) the password to use to derive the key |
salt |
(required) the salt |
iter |
(required) iteration count |
keylen |
(required) desired length of derived key |
hashfunc |
(optional) the hash function to use - must be one of the allowed functions (either in the SHA-1 or SHA-2 sets). Defaults to SHA-1 |
Examples
crypto.RSADecrypt
Decrypt an RSA-encrypted input and print the output as a string. Note that
this may result in unreadable text if the decrypted payload is binary. See
crypto.RSADecryptBytes for a safer method.
The private key must be a PEM-encoded RSA private key in PKCS#1, ASN.1 DER
form, which typically begins with -----BEGIN RSA PRIVATE KEY-----.
The input text must be plain ciphertext, as a byte array, or safely
convertible to a byte array. To decrypt base64-encoded input, you must
first decode with the base64.DecodeBytes
function.
Added in gomplate v3.8.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the private key to decrypt the input with |
input |
(required) the encrypted input |
Examples
crypto.RSADecryptBytes
Decrypt an RSA-encrypted input and output the decrypted byte array.
The private key must be a PEM-encoded RSA private key in PKCS#1, ASN.1 DER
form, which typically begins with -----BEGIN RSA PRIVATE KEY-----.
The input text must be plain ciphertext, as a byte array, or safely
convertible to a byte array. To decrypt base64-encoded input, you must
first decode with the base64.DecodeBytes
function.
See crypto.RSADecrypt for a function that outputs
a string.
Added in gomplate v3.8.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the private key to decrypt the input with |
input |
(required) the encrypted input |
Examples
crypto.RSAEncrypt
Encrypt the input with RSA and the padding scheme from PKCS#1 v1.5.
This function is suitable for encrypting data that will be decrypted by
Terraform’s rsadecrypt function.
The key should be a PEM-encoded RSA public key in PKIX ASN.1 DER form,
which typically begins with BEGIN PUBLIC KEY. RSA public keys in PKCS#1
ASN.1 DER form are also supported (beginning with RSA PUBLIC KEY).
The output will not be encoded, so consider base64-encoding it for display.
Note: Output encrypted with this function will not be deterministic, so encrypting the same input twice will not result in the same ciphertext.
Warning: Using this function may not be safe. See the warning on Go’s
rsa.EncryptPKCS1v15
documentation.
Added in gomplate v3.8.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the public key to encrypt the input with |
input |
(required) the encrypted input |
Examples
crypto.RSAGenerateKey
Generate a new RSA Private Key and output in PEM-encoded PKCS#1 ASN.1 DER form.
Default key length is 4096 bits, which should be safe enough for most
uses, but can be overridden with the optional bits parameter.
In order to protect against CWE-326,
keys shorter than 2048 bits may not be generated.
The output is a string, suitable for use with the other crypto.RSA*
functions.
Added in gomplate v3.8.0
Usage
Arguments
| name | description |
|---|---|
bits |
(optional) Length in bits of the generated key. Must be at least 2048. Defaults to 4096 |
Examples
crypto.RSADerivePublicKey
Derive a public key from an RSA private key and output in PKIX ASN.1 DER form.
The output is a string, suitable for use with other crypto.RSA*
functions.
Added in gomplate v3.8.0
Usage
Arguments
| name | description |
|---|---|
key |
(required) the private key to derive a public key from |
Examples
crypto.SHA*
Compute a checksum with a SHA-1 or SHA-2 algorithm as defined in RFC 3174 (SHA-1) and FIPS 180-4 (SHA-2).
These functions output the binary result as a hexadecimal string.
Warning: SHA-1 is cryptographically broken and should not be used for secure applications.
Added in gomplate v2.3.0
Usage
Arguments
| name | description |
|---|---|
input |
(required) the data to hash - can be binary data or text |
Examples
crypto.SHA*Bytes
Compute a checksum with a SHA-1 or SHA-2 algorithm as defined in RFC 3174 (SHA-1) and FIPS 180-4 (SHA-2).
These functions output the raw binary result, suitable for piping to other functions.
Warning: SHA-1 is cryptographically broken and should not be used for secure applications.
Added in gomplate v3.11.0
Usage
Arguments
| name | description |
|---|---|
input |
(required) the data to hash - can be binary data or text |
Examples
crypto.WPAPSK
This is really an alias to crypto.PBKDF2 with the
values necessary to convert ASCII passphrases to the WPA pre-shared keys for use with WiFi networks.
This can be used, for example, to help generate a configuration for wpa_supplicant.
Added in gomplate v2.3.0
Usage
Arguments
| name | description |
|---|---|
ssid |
(required) the WiFi SSID (network name) - must be less than 32 characters |
password |
(required) the password - must be between 8 and 63 characters |