-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt.go
102 lines (91 loc) · 2.8 KB
/
crypt.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// This file is part of clipsync (C)2023 by Marco Paganini
// Please see http://github.com/marcopaganini/clipsync for details.
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
mrand "math/rand"
"time"
)
const cryptKeyLen = 32
// newGCM creates a new cipher and GCM with the given key, returning the
// gcm object returned by cipher.NewGCM.
func newGCM(key []byte) (cipher.AEAD, error) {
if len(key) != cryptKeyLen {
return nil, fmt.Errorf("Key must be exactly %d bytes long", cryptKeyLen)
}
c, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("Error creating cipher: %v", err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, fmt.Errorf("Error creating GCM: %v", err)
}
return gcm, nil
}
// encrypt returns a copy of the cleartext string encrypted with AES256.
func encrypt(cleartext string, key []byte) (string, error) {
gcm, err := newGCM(key)
if err != nil {
return "", err
}
// Create a new random nonce.
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", fmt.Errorf("Error creating nonce: %v", err)
}
return string(gcm.Seal(nonce, nonce, []byte(cleartext), nil)), nil
}
// decrypt returns a copy of the decrypted ciphertext.
func decrypt(ciphertext string, key []byte) (string, error) {
gcm, err := newGCM(key)
if err != nil {
return "", err
}
nonceSize := gcm.NonceSize()
if nonceSize > len(ciphertext) {
return "", fmt.Errorf("nonce is longer than encrypted text")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
cleartext, err := gcm.Open(nil, []byte(nonce), []byte(ciphertext), nil)
if err != nil {
return "", fmt.Errorf("Error decrypting text: %v", err)
}
return string(cleartext), nil
}
// encrypt64 encrypts a copy of cleartext and returns a base64 encoded ciphertext.
func encrypt64(cleartext string, key []byte) (string, error) {
ciphertext, err := encrypt(cleartext, key)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString([]byte(ciphertext)), nil
}
// decrypt decrypts a base64 encoded ciphertext and returns the plain cleartext.
func decrypt64(ciphertext string, key []byte) (string, error) {
c, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", fmt.Errorf("Error decoding base64 encrypted text: %v", err)
}
cleartext, err := decrypt(string(c), key)
if err != nil {
return "", err
}
return cleartext, nil
}
// createPassword creates a 32-byte random password.
func createPassword() []byte {
charset := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()-_+="
ret := [cryptKeyLen]byte{}
mrand.Seed(time.Now().UnixNano())
clen := len(charset)
for i := 0; i < cryptKeyLen; i++ {
ret[i] = charset[mrand.Intn(clen)]
}
return ret[0:cryptKeyLen]
}