mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
allow option to skip encryption
This commit is contained in:
parent
a17f3096a0
commit
9223fc79e9
2 changed files with 28 additions and 0 deletions
|
@ -17,7 +17,12 @@ type Encryption struct {
|
||||||
|
|
||||||
// New generates a new Encryption, using the supplied passphrase and
|
// New generates a new Encryption, using the supplied passphrase and
|
||||||
// an optional supplied salt.
|
// an optional supplied salt.
|
||||||
|
// Passing nil passphrase will not use decryption.
|
||||||
func New(passphrase []byte, salt []byte) (e Encryption, err error) {
|
func New(passphrase []byte, salt []byte) (e Encryption, err error) {
|
||||||
|
if passphrase == nil {
|
||||||
|
e = Encryption{nil, nil, nil}
|
||||||
|
return
|
||||||
|
}
|
||||||
e.passphrase = passphrase
|
e.passphrase = passphrase
|
||||||
if salt == nil {
|
if salt == nil {
|
||||||
e.salt = make([]byte, 8)
|
e.salt = make([]byte, 8)
|
||||||
|
@ -37,6 +42,10 @@ func (e Encryption) Salt() []byte {
|
||||||
|
|
||||||
// Encrypt will generate an Encryption, prefixed with the IV
|
// Encrypt will generate an Encryption, prefixed with the IV
|
||||||
func (e Encryption) Encrypt(plaintext []byte) (encrypted []byte, err error) {
|
func (e Encryption) Encrypt(plaintext []byte) (encrypted []byte, err error) {
|
||||||
|
if e.passphrase == nil {
|
||||||
|
encrypted = plaintext
|
||||||
|
return
|
||||||
|
}
|
||||||
// generate a random iv each time
|
// generate a random iv each time
|
||||||
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
||||||
// Section 8.2
|
// Section 8.2
|
||||||
|
@ -57,6 +66,10 @@ func (e Encryption) Encrypt(plaintext []byte) (encrypted []byte, err error) {
|
||||||
|
|
||||||
// Decrypt an Encryption
|
// Decrypt an Encryption
|
||||||
func (e Encryption) Decrypt(encrypted []byte) (plaintext []byte, err error) {
|
func (e Encryption) Decrypt(encrypted []byte) (plaintext []byte, err error) {
|
||||||
|
if e.passphrase == nil {
|
||||||
|
plaintext = encrypted
|
||||||
|
return
|
||||||
|
}
|
||||||
b, err := aes.NewCipher(e.key)
|
b, err := aes.NewCipher(e.key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -44,3 +44,18 @@ func TestEncryption(t *testing.T) {
|
||||||
assert.NotEqual(t, dec, []byte("hello, world"))
|
assert.NotEqual(t, dec, []byte("hello, world"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func TestNoEncryption(t *testing.T) {
|
||||||
|
bob, err := New(nil, nil)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
jane, err := New(nil,nil)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
enc, err := bob.Encrypt([]byte("hello, world"))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
dec, err := jane.Decrypt(enc)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, dec, []byte("hello, world"))
|
||||||
|
assert.Equal(t, enc, []byte("hello, world"))
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue