mirror of https://github.com/schollz/croc.git
aes -> pbkdf2
This commit is contained in:
parent
302db87079
commit
e59df2e617
86
crypto.go
86
crypto.go
|
@ -1,19 +1,20 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/schollz/mnemonicode"
|
"github.com/schollz/mnemonicode"
|
||||||
|
"golang.org/x/crypto/pbkdf2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -28,62 +29,39 @@ func GetRandomName() string {
|
||||||
return strings.Join(result, "-")
|
return strings.Join(result, "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Encrypt(plaintext []byte, key string) (ciphertext []byte, err error) {
|
func Encrypt(plaintext []byte, passphrase string) (ciphertext []byte, err error) {
|
||||||
newKey := ""
|
key, salt := deriveKey(passphrase, nil)
|
||||||
for i := 0; i < 32; i++ {
|
iv := make([]byte, 12)
|
||||||
if i < len(key) {
|
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
||||||
newKey += string(key[i])
|
// Section 8.2
|
||||||
} else {
|
rand.Read(iv)
|
||||||
newKey += ":"
|
b, _ := aes.NewCipher(key)
|
||||||
}
|
aesgcm, _ := cipher.NewGCM(b)
|
||||||
}
|
data := aesgcm.Seal(nil, iv, plaintext, nil)
|
||||||
block, err := aes.NewCipher([]byte(newKey))
|
ciphertext = []byte(hex.EncodeToString(salt) + "-" + hex.EncodeToString(iv) + "-" + hex.EncodeToString(data))
|
||||||
if err != nil {
|
return
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
gcm, err := cipher.NewGCM(block)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
nonce := make([]byte, gcm.NonceSize())
|
|
||||||
_, err = io.ReadFull(rand.Reader, nonce)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return gcm.Seal(nonce, nonce, plaintext, nil), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Decrypt(ciphertext []byte, key string) (plaintext []byte, err error) {
|
func Decrypt(ciphertext []byte, passphrase string) (plaintext []byte, err error) {
|
||||||
newKey := ""
|
arr := bytes.Split(ciphertext, []byte("-"))
|
||||||
for i := 0; i < 32; i++ {
|
salt, _ := hex.DecodeString(string(arr[0]))
|
||||||
if i < len(key) {
|
iv, _ := hex.DecodeString(string(arr[1]))
|
||||||
newKey += string(key[i])
|
data, _ := hex.DecodeString(string(arr[2]))
|
||||||
} else {
|
key, _ := deriveKey(passphrase, salt)
|
||||||
newKey += ":"
|
b, _ := aes.NewCipher(key)
|
||||||
}
|
aesgcm, _ := cipher.NewGCM(b)
|
||||||
}
|
plaintext, err = aesgcm.Open(nil, iv, data, nil)
|
||||||
block, err := aes.NewCipher([]byte(newKey))
|
return
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
gcm, err := cipher.NewGCM(block)
|
func deriveKey(passphrase string, salt []byte) ([]byte, []byte) {
|
||||||
if err != nil {
|
if salt == nil {
|
||||||
return nil, err
|
salt = make([]byte, 8)
|
||||||
|
// http://www.ietf.org/rfc/rfc2898.txt
|
||||||
|
// Salt.
|
||||||
|
rand.Read(salt)
|
||||||
}
|
}
|
||||||
|
return pbkdf2.Key([]byte(passphrase), salt, 1000, 32, sha256.New), salt
|
||||||
if len(ciphertext) < gcm.NonceSize() {
|
|
||||||
return nil, errors.New("malformed ciphertext")
|
|
||||||
}
|
|
||||||
|
|
||||||
return gcm.Open(nil,
|
|
||||||
ciphertext[:gcm.NonceSize()],
|
|
||||||
ciphertext[gcm.NonceSize():],
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Hash(data string) string {
|
func Hash(data string) string {
|
||||||
|
|
Loading…
Reference in New Issue