forked from jmcfarlane/notable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.go
51 lines (43 loc) · 1.3 KB
/
encryption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"crypto/sha256"
"errors"
"regexp"
"github.com/gtank/cryptopasta"
)
var (
// Decrypted strings generally look like this
decryptedRE = regexp.MustCompile(`[\x00-\x7F]`)
// Encrypted string generally look like this
encryptedRE = regexp.MustCompile(`[^\x00-\x7F]`)
)
const (
aesGcm = "AES-GCM"
)
// SmellsEncrypted - Try to guess if a string is encrypted or not
func SmellsEncrypted(content string) bool {
decrypted := len(decryptedRE.FindAllString(content, -1))
encrypted := len(encryptedRE.FindAllString(content, -1))
if float64(encrypted)/float64(decrypted) > 0.4 {
return true
}
return false
}
// Decrypt using the correct cipher type
func Decrypt(note Note, password string) (string, error) {
if note.CipherType == aesGcm {
key := sha256.Sum256([]byte(password))
clearText, err := cryptopasta.Decrypt([]byte(note.Content), &key)
return string(clearText), err
}
return CBCDecrypt(note.Content, password)
}
// Encrypt a note using the currently desired mechanism: AES-GCM
func Encrypt(note Note) (string, string, error) {
if note.Password == "" {
return "", "", errors.New("Cannot encrypt with an empty password")
}
key := sha256.Sum256([]byte(note.Password))
cipherText, err := cryptopasta.Encrypt([]byte(note.Content), &key)
return string(cipherText), aesGcm, err
}