-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package app | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha512" | ||
"strings" | ||
|
||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
|
||
/* | ||
https://golang.org/src/crypto/cipher/example_test.go | ||
*/ | ||
|
||
const nonceSize = 12 | ||
const saltSize = 16 | ||
|
||
// Encrypt encrypts a file with a password | ||
func Encrypt(plainText []byte, password string) ([]byte, error) { | ||
|
||
passwordBytes := []byte(password) | ||
|
||
salt := make([]byte, saltSize) | ||
_, err := rand.Read(salt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key := pbkdf2.Key(passwordBytes, salt, 4096, 32, sha512.New) | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nonce := make([]byte, nonceSize) | ||
_, err = rand.Read(nonce) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cipherText := aesgcm.Seal(nil, nonce, plainText, nil) | ||
cipherText = append(cipherText, salt...) | ||
cipherText = append(cipherText, nonce...) | ||
|
||
return cipherText, nil | ||
} | ||
|
||
// Decrypt decrypts a file with a given password | ||
func Decrypt(cipherText []byte, password string) ([]byte, error) { | ||
|
||
passwordBytes := []byte(password) | ||
|
||
ctLen := len(cipherText) | ||
nonce := cipherText[ctLen-nonceSize:] | ||
salt := cipherText[ctLen-nonceSize-saltSize : ctLen-nonceSize] | ||
|
||
key := pbkdf2.Key(passwordBytes, salt, 4096, 32, sha512.New) | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
plainText, err := aesgcm.Open( | ||
nil, | ||
nonce, | ||
cipherText[:ctLen-saltSize-nonceSize], | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return plainText, nil | ||
} | ||
|
||
// InvalidPassword is returned from Decrypt if an invalid password is used to | ||
// decrypt the ciphertext | ||
func InvalidPassword(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
return strings.Compare(err.Error(), "cipher: message authentication failed") == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package app_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ubclaunchpad/cumulus/app" | ||
) | ||
|
||
func TestInvalidPassword(t *testing.T) { | ||
plainText := "Hello, world!" | ||
plainTextBytes := []byte(plainText) | ||
|
||
cipherText, _ := app.Encrypt(plainTextBytes, "correctPassword") | ||
plainTextNew, err := app.Decrypt(cipherText, "incorrectPassword") | ||
|
||
if !app.InvalidPassword(err) { | ||
t.Fail() | ||
} | ||
|
||
if plainTextNew != nil { | ||
t.Fail() | ||
} | ||
|
||
plainTextNew, err = app.Decrypt(cipherText, "correctPassword") | ||
|
||
if string(plainTextNew[:]) != plainText { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.