2015-07-17 21:49:39 +00:00
|
|
|
package openssl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2019-04-29 14:34:58 +00:00
|
|
|
"strings"
|
2015-07-17 21:49:39 +00:00
|
|
|
"testing"
|
2023-08-22 12:11:34 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testPassphrase = "z4yH36a6zerhfE5427ZV" //#nosec G101 -- Is hardcoded passphrase but only for testing purposes
|
|
|
|
testPlaintext = "hallowelt"
|
2015-07-17 21:49:39 +00:00
|
|
|
)
|
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
var testTable = []struct {
|
|
|
|
tName string
|
|
|
|
tMdParam string
|
2020-06-13 13:29:46 +00:00
|
|
|
tMdFunc CredsGenerator
|
|
|
|
tPBKDF bool
|
2019-04-29 14:34:58 +00:00
|
|
|
}{
|
2020-06-13 13:29:46 +00:00
|
|
|
{"MD5", "md5", BytesToKeyMD5, false},
|
|
|
|
{"SHA1", "sha1", BytesToKeySHA1, false},
|
|
|
|
{"SHA256", "sha256", BytesToKeySHA256, false},
|
2020-06-13 16:12:42 +00:00
|
|
|
{"SHA384", "sha384", BytesToKeySHA384, false},
|
|
|
|
{"SHA512", "sha512", BytesToKeySHA512, false},
|
2020-06-13 13:29:46 +00:00
|
|
|
{"PBKDF2_MD5", "md5", PBKDF2MD5, true},
|
|
|
|
{"PBKDF2_SHA1", "sha1", PBKDF2SHA1, true},
|
|
|
|
{"PBKDF2_SHA256", "sha256", PBKDF2SHA256, true},
|
2020-06-13 16:12:42 +00:00
|
|
|
{"PBKDF2_SHA384", "sha384", PBKDF2SHA384, true},
|
|
|
|
{"PBKDF2_SHA512", "sha512", PBKDF2SHA512, true},
|
2015-07-17 21:49:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
func TestBinaryEncryptToDecryptWithCustomSalt(t *testing.T) {
|
|
|
|
salt := []byte("saltsalt")
|
|
|
|
|
|
|
|
o := New()
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
enc, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(testPassphrase, salt, []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
dec, err := o.DecryptBinaryBytes(testPassphrase, enc, BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, testPlaintext, string(dec))
|
2019-04-29 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBinaryEncryptToDecrypt(t *testing.T) {
|
|
|
|
o := New()
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
enc, err := o.EncryptBinaryBytes(testPassphrase, []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
dec, err := o.DecryptBinaryBytes(testPassphrase, enc, BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, testPlaintext, string(dec))
|
2019-04-29 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBinaryEncryptToOpenSSL(t *testing.T) {
|
2018-09-11 08:56:37 +00:00
|
|
|
o := New()
|
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.tName, func(t *testing.T) {
|
2019-04-29 14:48:46 +00:00
|
|
|
salt, err := o.GenerateSalt()
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
enc, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(testPassphrase, salt, []byte(testPlaintext), tc.tMdFunc)
|
|
|
|
require.NoError(t, err)
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
// Need to specify /dev/stdin as file so that we can pass in binary
|
|
|
|
// data to openssl without creating a file
|
2020-06-13 13:29:46 +00:00
|
|
|
cmdArgs := []string{
|
2019-04-29 14:34:58 +00:00
|
|
|
"openssl", "aes-256-cbc",
|
2019-04-29 14:48:46 +00:00
|
|
|
"-d",
|
2023-08-22 12:11:34 +00:00
|
|
|
"-pass", fmt.Sprintf("pass:%s", testPassphrase),
|
2019-04-29 14:34:58 +00:00
|
|
|
"-md", tc.tMdParam,
|
2019-04-29 14:48:46 +00:00
|
|
|
"-in", "/dev/stdin",
|
2020-06-13 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.tPBKDF {
|
|
|
|
cmdArgs = append(cmdArgs, "-pbkdf2")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec:G204 -- Hardcoded tests, this is fine
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stdin = bytes.NewBuffer(enc)
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
err = cmd.Run()
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, testPlaintext, out.String())
|
2019-04-29 14:34:58 +00:00
|
|
|
})
|
2018-09-11 08:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
func TestBinaryEncryptWithSaltShouldHaveSameOutput(t *testing.T) {
|
|
|
|
plaintext := "outputshouldbesame"
|
|
|
|
passphrase := "passphrasesupersecure"
|
|
|
|
salt := []byte("saltsalt")
|
|
|
|
|
|
|
|
o := New()
|
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
enc1, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), BytesToKeySHA256)
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
enc2, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), BytesToKeySHA256)
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-29 14:48:46 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, enc1, enc2)
|
2019-04-29 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 13:37:29 +00:00
|
|
|
func TestDecryptBinaryFromString(t *testing.T) {
|
|
|
|
o := New()
|
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.tName, func(t *testing.T) {
|
2019-04-29 13:37:29 +00:00
|
|
|
var out bytes.Buffer
|
2019-04-29 14:34:58 +00:00
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
cmdArgs := []string{
|
2019-04-29 14:34:58 +00:00
|
|
|
"openssl", "aes-256-cbc",
|
2023-08-22 12:11:34 +00:00
|
|
|
"-pass", fmt.Sprintf("pass:%s", testPassphrase),
|
2019-04-29 14:34:58 +00:00
|
|
|
"-md", tc.tMdParam,
|
|
|
|
"-in", "/dev/stdin",
|
2020-06-13 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.tPBKDF {
|
|
|
|
cmdArgs = append(cmdArgs, "-pbkdf2")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec:G204 -- Hardcoded tests, this is fine
|
2019-04-29 13:37:29 +00:00
|
|
|
cmd.Stdout = &out
|
2023-08-22 12:11:34 +00:00
|
|
|
cmd.Stdin = strings.NewReader(testPlaintext)
|
2019-04-29 14:34:58 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, cmd.Run())
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
data, err := o.DecryptBinaryBytes(testPassphrase, out.Bytes(), tc.tMdFunc)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
if !assert.Equal(t, testPlaintext, string(data)) {
|
|
|
|
t.Logf("Data: %s\nPlaintext: %s", string(data), testPlaintext)
|
2019-04-29 13:37:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
func TestDecryptFromString(t *testing.T) {
|
2015-07-17 21:49:39 +00:00
|
|
|
o := New()
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.tName, func(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
cmdArgs := []string{
|
2019-04-29 14:48:46 +00:00
|
|
|
"openssl", "aes-256-cbc",
|
|
|
|
"-base64",
|
2023-08-22 12:11:34 +00:00
|
|
|
"-pass", fmt.Sprintf("pass:%s", testPassphrase),
|
2019-04-29 14:48:46 +00:00
|
|
|
"-md", tc.tMdParam,
|
2020-06-13 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.tPBKDF {
|
|
|
|
cmdArgs = append(cmdArgs, "-pbkdf2")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec:G204 -- Hardcoded tests, this is fine
|
2019-04-29 14:48:46 +00:00
|
|
|
cmd.Stdout = &out
|
2023-08-22 12:11:34 +00:00
|
|
|
cmd.Stdin = strings.NewReader(testPlaintext)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, cmd.Run())
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
data, err := o.DecryptBytes(testPassphrase, out.Bytes(), tc.tMdFunc)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
if !assert.Equal(t, testPlaintext, string(data)) {
|
|
|
|
t.Logf("Data: %s\nPlaintext: %s", string(data), testPlaintext)
|
2019-04-29 14:48:46 +00:00
|
|
|
}
|
|
|
|
})
|
2019-04-29 13:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
func TestEncryptToDecrypt(t *testing.T) {
|
2017-09-18 13:14:20 +00:00
|
|
|
o := New()
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
enc, err := o.EncryptBytes(testPassphrase, []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2017-09-18 13:14:20 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
dec, err := o.DecryptBytes(testPassphrase, enc, BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2017-09-18 13:14:20 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, testPlaintext, string(dec))
|
2017-09-18 13:14:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
func TestEncryptToDecryptWithCustomSalt(t *testing.T) {
|
2019-04-29 13:37:29 +00:00
|
|
|
salt := []byte("saltsalt")
|
|
|
|
|
|
|
|
o := New()
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
enc, err := o.EncryptBytesWithSaltAndDigestFunc(testPassphrase, salt, []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
dec, err := o.DecryptBytes(testPassphrase, enc, BytesToKeySHA256)
|
|
|
|
require.NoError(t, err)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, testPlaintext, string(dec))
|
2019-04-29 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 21:49:39 +00:00
|
|
|
func TestEncryptToOpenSSL(t *testing.T) {
|
2019-04-29 14:34:58 +00:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.tName, func(t *testing.T) {
|
|
|
|
o := New()
|
2015-07-17 21:49:39 +00:00
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
salt, err := o.GenerateSalt()
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2015-07-17 21:49:39 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
enc, err := o.EncryptBytesWithSaltAndDigestFunc(testPassphrase, salt, []byte(testPlaintext), tc.tMdFunc)
|
|
|
|
require.NoError(t, err)
|
2015-07-17 21:49:39 +00:00
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
enc = append(enc, '\n')
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
var out bytes.Buffer
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
cmdArgs := []string{
|
2019-04-29 14:34:58 +00:00
|
|
|
"openssl", "aes-256-cbc",
|
|
|
|
"-base64", "-d",
|
2023-08-22 12:11:34 +00:00
|
|
|
"-pass", fmt.Sprintf("pass:%s", testPassphrase),
|
2019-04-29 14:34:58 +00:00
|
|
|
"-md", tc.tMdParam,
|
|
|
|
"-in", "/dev/stdin",
|
2020-06-13 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.tPBKDF {
|
|
|
|
cmdArgs = append(cmdArgs, "-pbkdf2")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec:G204 -- Hardcoded tests, this is fine
|
2019-04-29 14:34:58 +00:00
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stdin = bytes.NewReader(enc)
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, cmd.Run())
|
2018-09-11 08:56:37 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, testPlaintext, out.String())
|
2019-04-29 14:34:58 +00:00
|
|
|
})
|
2015-07-17 21:49:39 +00:00
|
|
|
}
|
2018-09-11 08:56:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
func TestEncryptWithSaltShouldHaveSameOutput(t *testing.T) {
|
|
|
|
plaintext := "outputshouldbesame"
|
|
|
|
passphrase := "passphrasesupersecure"
|
|
|
|
salt := []byte("saltsalt")
|
2019-04-29 13:37:29 +00:00
|
|
|
|
|
|
|
o := New()
|
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
enc1, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), BytesToKeySHA256)
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
enc2, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), BytesToKeySHA256)
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-29 13:37:29 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.Equal(t, enc1, enc2)
|
2019-04-29 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 08:56:37 +00:00
|
|
|
func TestGenerateSalt(t *testing.T) {
|
|
|
|
knownSalts := [][]byte{}
|
|
|
|
|
|
|
|
o := New()
|
2015-07-17 21:49:39 +00:00
|
|
|
|
2019-04-29 14:34:58 +00:00
|
|
|
for i := 0; i < 1000; i++ {
|
2018-09-11 08:56:37 +00:00
|
|
|
salt, err := o.GenerateSalt()
|
2023-08-22 12:11:34 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-11 08:56:37 +00:00
|
|
|
|
|
|
|
for _, ks := range knownSalts {
|
2023-08-22 12:11:34 +00:00
|
|
|
assert.NotEqual(t, ks, salt)
|
2018-09-11 08:56:37 +00:00
|
|
|
knownSalts = append(knownSalts, salt)
|
|
|
|
}
|
2015-07-17 21:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-18 13:49:33 +00:00
|
|
|
|
|
|
|
func TestSaltValidation(t *testing.T) {
|
2023-08-22 12:11:34 +00:00
|
|
|
var err error
|
2017-09-18 13:49:33 +00:00
|
|
|
o := New()
|
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
_, err = o.EncryptBytesWithSaltAndDigestFunc(testPassphrase, []byte("12345"), []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
assert.ErrorIs(t, err, ErrInvalidSalt)
|
2017-09-18 13:49:33 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
_, err = o.EncryptBytesWithSaltAndDigestFunc(testPassphrase, []byte("1234567890"), []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
assert.ErrorIs(t, err, ErrInvalidSalt)
|
2017-09-18 13:49:33 +00:00
|
|
|
|
2023-08-22 12:11:34 +00:00
|
|
|
_, err = o.EncryptBytesWithSaltAndDigestFunc(testPassphrase, []byte{0xcb, 0xd5, 0x1a, 0x3, 0x84, 0xba, 0xa8, 0xc8}, []byte(testPlaintext), BytesToKeySHA256)
|
|
|
|
assert.NoError(t, err)
|
2017-09-18 13:49:33 +00:00
|
|
|
}
|
2018-09-11 10:11:16 +00:00
|
|
|
|
2019-04-29 14:48:46 +00:00
|
|
|
//
|
|
|
|
// Benchmarks
|
|
|
|
//
|
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
func benchmarkDecrypt(ciphertext []byte, cg CredsGenerator, b *testing.B) {
|
2018-09-11 10:11:16 +00:00
|
|
|
o := New()
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
2023-08-22 12:11:34 +00:00
|
|
|
_, err := o.DecryptBytes(testPassphrase, ciphertext, cg)
|
|
|
|
require.NoError(b, err)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecryptMD5(b *testing.B) {
|
2020-06-13 13:29:46 +00:00
|
|
|
benchmarkDecrypt([]byte("U2FsdGVkX19ZM5qQJGe/d5A/4pccgH+arBGTp+QnWPU="), BytesToKeyMD5, b)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecryptSHA1(b *testing.B) {
|
2020-06-13 13:29:46 +00:00
|
|
|
benchmarkDecrypt([]byte("U2FsdGVkX1/Yy9kegseq2Ewd4UvjFYCpIEA1cltTA1Q="), BytesToKeySHA1, b)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecryptSHA256(b *testing.B) {
|
2020-06-13 13:29:46 +00:00
|
|
|
benchmarkDecrypt([]byte("U2FsdGVkX1+O68d7BO9ibP8nB5+xtb/27IHlyjJWpl8="), BytesToKeySHA256, b)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:29:46 +00:00
|
|
|
func benchmarkEncrypt(plaintext string, cg CredsGenerator, b *testing.B) {
|
2018-09-11 10:11:16 +00:00
|
|
|
o := New()
|
|
|
|
salt, _ := o.GenerateSalt()
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
2023-08-22 12:11:34 +00:00
|
|
|
_, err := o.EncryptBytesWithSaltAndDigestFunc(testPassphrase, salt, []byte(plaintext), cg)
|
|
|
|
require.NoError(b, err)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncryptMD5(b *testing.B) {
|
2023-08-22 12:11:34 +00:00
|
|
|
benchmarkEncrypt(testPlaintext, BytesToKeyMD5, b)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncryptSHA1(b *testing.B) {
|
2023-08-22 12:11:34 +00:00
|
|
|
benchmarkEncrypt(testPlaintext, BytesToKeySHA1, b)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncryptSHA256(b *testing.B) {
|
2023-08-22 12:11:34 +00:00
|
|
|
benchmarkEncrypt(testPlaintext, BytesToKeySHA256, b)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGenerateSalt(b *testing.B) {
|
|
|
|
o := New()
|
|
|
|
for n := 0; n < b.N; n++ {
|
2023-08-22 12:11:34 +00:00
|
|
|
_, err := o.GenerateSalt()
|
|
|
|
require.NoError(b, err)
|
2018-09-11 10:11:16 +00:00
|
|
|
}
|
|
|
|
}
|