Skip to content

Commit 9dbb83e

Browse files
committed
Moved the encryption to its own package
1 parent 228b324 commit 9dbb83e

File tree

6 files changed

+54
-45
lines changed

6 files changed

+54
-45
lines changed

encrypt/encrypt.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package encrypt
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
7+
"github.com/schollz/cryptopasta"
8+
)
9+
10+
func EncryptString(toEncrypt string, password string) (string, error) {
11+
key := sha256.Sum256([]byte(password))
12+
encrypted, err := cryptopasta.Encrypt([]byte(toEncrypt), &key)
13+
if err != nil {
14+
return "", err
15+
}
16+
17+
return hex.EncodeToString(encrypted), nil
18+
}
19+
20+
func DecryptString(toDecrypt string, password string) (string, error) {
21+
key := sha256.Sum256([]byte(password))
22+
contentData, err := hex.DecodeString(toDecrypt)
23+
if err != nil {
24+
return "", err
25+
}
26+
bDecrypted, err := cryptopasta.Decrypt(contentData, &key)
27+
return string(bDecrypted), err
28+
}

encrypt/encrypt_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package encrypt
2+
3+
import "testing"
4+
5+
func TestEncryption(t *testing.T) {
6+
s, err := EncryptString("some string", "some password")
7+
if err != nil {
8+
t.Errorf("What")
9+
}
10+
d, err := DecryptString(s, "some wrong password")
11+
if err == nil {
12+
t.Errorf("Should throw error for bad password")
13+
}
14+
d, err = DecryptString(s, "some password")
15+
if err != nil {
16+
t.Errorf("Should not throw password")
17+
}
18+
if d != "some string" {
19+
t.Errorf("Problem decoding")
20+
}
21+
22+
}

handlers.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// "github.com/gin-contrib/static"
1111
"github.com/gin-contrib/multitemplate"
1212
"github.com/gin-gonic/gin"
13+
"github.com/schollz/cowyo/encrypt"
1314
)
1415

1516
func serve(host, port, crt_path, key_path string, TLS bool) {
@@ -328,7 +329,7 @@ func handleEncrypt(c *gin.Context) {
328329
q := Open(json.Page)
329330
var message string
330331
if p.IsEncrypted {
331-
decrypted, err2 := DecryptString(p.Text.GetCurrent(), json.Passphrase)
332+
decrypted, err2 := encrypt.DecryptString(p.Text.GetCurrent(), json.Passphrase)
332333
if err2 != nil {
333334
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"})
334335
return
@@ -342,7 +343,7 @@ func handleEncrypt(c *gin.Context) {
342343
message = "Decrypted"
343344
} else {
344345
currentText := p.Text.GetCurrent()
345-
encrypted, _ := EncryptString(currentText, json.Passphrase)
346+
encrypted, _ := encrypt.EncryptString(currentText, json.Passphrase)
346347
q.Erase()
347348
q = Open(json.Page)
348349
q.Update(encrypted)

page_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestListFiles(t *testing.T) {
1717
p.Update("A different bunch of data")
1818
p = Open("testpage3")
1919
p.Update("Not much else")
20-
n, l := DirectoryList()
20+
n, l, _, _ := DirectoryList()
2121
if strings.Join(n, " ") != "testpage testpage2 testpage3" {
2222
t.Errorf("Names: %s, Lengths: %d", n, l)
2323
}

utils.go

-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"crypto/sha256"
54
"encoding/base32"
65
"encoding/binary"
76
"encoding/hex"
@@ -14,7 +13,6 @@ import (
1413
"github.com/jcelliott/lumber"
1514
"github.com/microcosm-cc/bluemonday"
1615
"github.com/russross/blackfriday"
17-
"github.com/schollz/cryptopasta"
1816
"github.com/shurcooL/github_flavored_markdown"
1917
"golang.org/x/crypto/bcrypt"
2018
)
@@ -160,26 +158,6 @@ func CheckPasswordHash(password, hashedString string) error {
160158
return bcrypt.CompareHashAndPassword(hash, []byte(password))
161159
}
162160

163-
func EncryptString(toEncrypt string, password string) (string, error) {
164-
key := sha256.Sum256([]byte(password))
165-
encrypted, err := cryptopasta.Encrypt([]byte(toEncrypt), &key)
166-
if err != nil {
167-
return "", err
168-
}
169-
170-
return hex.EncodeToString(encrypted), nil
171-
}
172-
173-
func DecryptString(toDecrypt string, password string) (string, error) {
174-
key := sha256.Sum256([]byte(password))
175-
contentData, err := hex.DecodeString(toDecrypt)
176-
if err != nil {
177-
return "", err
178-
}
179-
bDecrypted, err := cryptopasta.Decrypt(contentData, &key)
180-
return string(bDecrypted), err
181-
}
182-
183161
// exists returns whether the given file or directory exists or not
184162
func exists(path string) bool {
185163
_, err := os.Stat(path)

utils_test.go

-20
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,3 @@ func TestHashing(t *testing.T) {
3232
t.Errorf("Should NOT be correct password")
3333
}
3434
}
35-
36-
func TestEncryption(t *testing.T) {
37-
s, err := EncryptString("some string", "some password")
38-
if err != nil {
39-
t.Errorf("What")
40-
}
41-
log.Debug(s)
42-
d, err := DecryptString(s, "some wrong password")
43-
if err == nil {
44-
t.Errorf("Should throw error for bad password")
45-
}
46-
d, err = DecryptString(s, "some password")
47-
if err != nil {
48-
t.Errorf("Should not throw password")
49-
}
50-
if d != "some string" {
51-
t.Errorf("Problem decoding")
52-
}
53-
54-
}

0 commit comments

Comments
 (0)