From 9dbb83e8afeee92995ee730327afa83de9e0d6c6 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Thu, 29 Jun 2017 15:24:34 -0600 Subject: [PATCH] Moved the encryption to its own package --- encrypt/encrypt.go | 28 ++++++++++++++++++++++++++++ encrypt/encrypt_test.go | 22 ++++++++++++++++++++++ handlers.go | 5 +++-- page_test.go | 2 +- utils.go | 22 ---------------------- utils_test.go | 20 -------------------- 6 files changed, 54 insertions(+), 45 deletions(-) create mode 100644 encrypt/encrypt.go create mode 100644 encrypt/encrypt_test.go diff --git a/encrypt/encrypt.go b/encrypt/encrypt.go new file mode 100644 index 0000000..b59f5f8 --- /dev/null +++ b/encrypt/encrypt.go @@ -0,0 +1,28 @@ +package encrypt + +import ( + "crypto/sha256" + "encoding/hex" + + "github.com/schollz/cryptopasta" +) + +func EncryptString(toEncrypt string, password string) (string, error) { + key := sha256.Sum256([]byte(password)) + encrypted, err := cryptopasta.Encrypt([]byte(toEncrypt), &key) + if err != nil { + return "", err + } + + return hex.EncodeToString(encrypted), nil +} + +func DecryptString(toDecrypt string, password string) (string, error) { + key := sha256.Sum256([]byte(password)) + contentData, err := hex.DecodeString(toDecrypt) + if err != nil { + return "", err + } + bDecrypted, err := cryptopasta.Decrypt(contentData, &key) + return string(bDecrypted), err +} diff --git a/encrypt/encrypt_test.go b/encrypt/encrypt_test.go new file mode 100644 index 0000000..d5f379c --- /dev/null +++ b/encrypt/encrypt_test.go @@ -0,0 +1,22 @@ +package encrypt + +import "testing" + +func TestEncryption(t *testing.T) { + s, err := EncryptString("some string", "some password") + if err != nil { + t.Errorf("What") + } + d, err := DecryptString(s, "some wrong password") + if err == nil { + t.Errorf("Should throw error for bad password") + } + d, err = DecryptString(s, "some password") + if err != nil { + t.Errorf("Should not throw password") + } + if d != "some string" { + t.Errorf("Problem decoding") + } + +} diff --git a/handlers.go b/handlers.go index bdf2c76..a686a3e 100755 --- a/handlers.go +++ b/handlers.go @@ -10,6 +10,7 @@ import ( // "github.com/gin-contrib/static" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" + "github.com/schollz/cowyo/encrypt" ) func serve(host, port, crt_path, key_path string, TLS bool) { @@ -328,7 +329,7 @@ func handleEncrypt(c *gin.Context) { q := Open(json.Page) var message string if p.IsEncrypted { - decrypted, err2 := DecryptString(p.Text.GetCurrent(), json.Passphrase) + decrypted, err2 := encrypt.DecryptString(p.Text.GetCurrent(), json.Passphrase) if err2 != nil { c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"}) return @@ -342,7 +343,7 @@ func handleEncrypt(c *gin.Context) { message = "Decrypted" } else { currentText := p.Text.GetCurrent() - encrypted, _ := EncryptString(currentText, json.Passphrase) + encrypted, _ := encrypt.EncryptString(currentText, json.Passphrase) q.Erase() q = Open(json.Page) q.Update(encrypted) diff --git a/page_test.go b/page_test.go index 7064466..3d089c1 100755 --- a/page_test.go +++ b/page_test.go @@ -17,7 +17,7 @@ func TestListFiles(t *testing.T) { p.Update("A different bunch of data") p = Open("testpage3") p.Update("Not much else") - n, l := DirectoryList() + n, l, _, _ := DirectoryList() if strings.Join(n, " ") != "testpage testpage2 testpage3" { t.Errorf("Names: %s, Lengths: %d", n, l) } diff --git a/utils.go b/utils.go index 7518aad..2e9c818 100644 --- a/utils.go +++ b/utils.go @@ -1,7 +1,6 @@ package main import ( - "crypto/sha256" "encoding/base32" "encoding/binary" "encoding/hex" @@ -14,7 +13,6 @@ import ( "github.com/jcelliott/lumber" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday" - "github.com/schollz/cryptopasta" "github.com/shurcooL/github_flavored_markdown" "golang.org/x/crypto/bcrypt" ) @@ -160,26 +158,6 @@ func CheckPasswordHash(password, hashedString string) error { return bcrypt.CompareHashAndPassword(hash, []byte(password)) } -func EncryptString(toEncrypt string, password string) (string, error) { - key := sha256.Sum256([]byte(password)) - encrypted, err := cryptopasta.Encrypt([]byte(toEncrypt), &key) - if err != nil { - return "", err - } - - return hex.EncodeToString(encrypted), nil -} - -func DecryptString(toDecrypt string, password string) (string, error) { - key := sha256.Sum256([]byte(password)) - contentData, err := hex.DecodeString(toDecrypt) - if err != nil { - return "", err - } - bDecrypted, err := cryptopasta.Decrypt(contentData, &key) - return string(bDecrypted), err -} - // exists returns whether the given file or directory exists or not func exists(path string) bool { _, err := os.Stat(path) diff --git a/utils_test.go b/utils_test.go index bc9561c..29dc9aa 100755 --- a/utils_test.go +++ b/utils_test.go @@ -32,23 +32,3 @@ func TestHashing(t *testing.T) { t.Errorf("Should NOT be correct password") } } - -func TestEncryption(t *testing.T) { - s, err := EncryptString("some string", "some password") - if err != nil { - t.Errorf("What") - } - log.Debug(s) - d, err := DecryptString(s, "some wrong password") - if err == nil { - t.Errorf("Should throw error for bad password") - } - d, err = DecryptString(s, "some password") - if err != nil { - t.Errorf("Should not throw password") - } - if d != "some string" { - t.Errorf("Problem decoding") - } - -}