mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Moved the encryption to its own package
This commit is contained in:
parent
228b324149
commit
9dbb83e8af
28
encrypt/encrypt.go
Normal file
28
encrypt/encrypt.go
Normal 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
22
encrypt/encrypt_test.go
Normal 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")
|
||||
}
|
||||
|
||||
}
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
22
utils.go
22
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)
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user