Skip to content

Commit

Permalink
correct/update exposed function name
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasen committed Dec 3, 2015
1 parent cb7a611 commit 9605a14
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions aes256cbc/openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func New() *OpenSSL {
}
}

// DecryptString decrypts a string that was encrypted using OpenSSL and AES-256-CBC
func (o *OpenSSL) DecryptString(passphrase, encryptedBase64String []byte) ([]byte, error) {
// Decrypt a string that was encrypted using OpenSSL and AES-256-CBC
// also compatible with crptojs
func (o *OpenSSL) Decrypt(passphrase, encryptedBase64String []byte) ([]byte, error) {
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(encryptedBase64String)))
n, err := base64.StdEncoding.Decode(dbuf, encryptedBase64String)
if err != nil {
Expand Down Expand Up @@ -67,9 +68,10 @@ func (o *OpenSSL) decrypt(key, iv, data []byte) ([]byte, error) {
return out, nil
}

// EncryptString encrypts a string in a manner compatible to OpenSSL encryption
// Encrypt a string in a manner compatible to OpenSSL encryption
// functions using AES-256-CBC as encryption algorithm
func (o *OpenSSL) EncryptString(passphrase, plaintextString []byte) ([]byte, error) {
// also compatible with crptojs from https://code.google.com/p/crypto-js/
func (o *OpenSSL) Encrypt(passphrase, plaintextString []byte) ([]byte, error) {
salt := make([]byte, 8) // Generate an 8 byte salt
_, err := io.ReadFull(rand.Reader, salt)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions aes256cbc/openssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestDecryptFromString(t *testing.T) {

o := New()

data, err := o.DecryptString(passphrase, opensslEncrypted)
data, err := o.Decrypt(passphrase, opensslEncrypted)

if err != nil {
t.Fatalf("Test errored: %s", err)
Expand All @@ -33,12 +33,12 @@ func TestEncryptToDecrypt(t *testing.T) {

o := New()

enc, err := o.EncryptString(passphrase, plaintext)
enc, err := o.Encrypt(passphrase, plaintext)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}

dec, err := o.DecryptString(passphrase, enc)
dec, err := o.Decrypt(passphrase, enc)
if err != nil {
t.Fatalf("Test errored at decrypt: %s", err)
}
Expand All @@ -54,7 +54,7 @@ func TestEncryptToOpenSSL(t *testing.T) {

o := New()

enc, err := o.EncryptString(passphrase, plaintext)
enc, err := o.Encrypt(passphrase, plaintext)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func decryptBackendAddr(line []byte) ([]byte, error) {
return addr, nil
}
// Try to decrypt it (AES)
addr, err := _Aes256CBC.DecryptString(_SecretPassphase, line)
addr, err := _Aes256CBC.Decrypt(_SecretPassphase, line)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestMain(m *testing.M) {
func TestTextDecryptAES(t *testing.T) {
o := aes256cbc.New()

dec, err := o.DecryptString(_secret, _expectAESCiphertext)
dec, err := o.Decrypt(_secret, _expectAESCiphertext)
if err != nil {
panic(err)
}
Expand All @@ -85,7 +85,7 @@ func TestTextDecryptAES(t *testing.T) {
func encryptText(plaintext, passphrase []byte) ([]byte, error) {
o := aes256cbc.New()

return o.EncryptString(passphrase, plaintext)
return o.Encrypt(passphrase, plaintext)
}

func randomBytes(n int) []byte {
Expand Down Expand Up @@ -186,7 +186,7 @@ func BenchmarkEncryptText(b *testing.B) {
func BenchmarkDecryptText(b *testing.B) {
for i := 0; i < b.N; i++ {
o := aes256cbc.New()
_, err := o.DecryptString(_secret, _expectAESCiphertext)
_, err := o.Decrypt(_secret, _expectAESCiphertext)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 9605a14

Please sign in to comment.