Moved the encryption to its own package

This commit is contained in:
Zack Scholl 2017-06-29 15:24:34 -06:00
parent 228b324149
commit 9dbb83e8af
6 changed files with 54 additions and 45 deletions

28
encrypt/encrypt.go Normal file
View File

@ -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
}

22
encrypt/encrypt_test.go Normal file
View File

@ -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")
}
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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)

View File

@ -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")
}
}