1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-28 09:35:14 +03:00
croc/crypto.go

126 lines
3.0 KiB
Go
Raw Normal View History

package main
import (
2017-10-18 07:23:31 +03:00
"crypto/aes"
"crypto/cipher"
"crypto/rand"
2017-10-20 04:01:07 +03:00
"crypto/sha1"
2017-10-18 05:33:27 +03:00
"crypto/sha256"
"encoding/binary"
2017-10-18 07:23:31 +03:00
"encoding/hex"
2017-10-18 05:33:27 +03:00
"fmt"
mathrand "math/rand"
2017-10-20 04:01:07 +03:00
"os"
"strings"
"time"
2017-10-20 04:01:07 +03:00
"github.com/mars9/crypt"
"github.com/schollz/mnemonicode"
2017-10-20 04:01:07 +03:00
log "github.com/sirupsen/logrus"
2017-10-18 05:41:52 +03:00
"golang.org/x/crypto/pbkdf2"
)
func init() {
mathrand.Seed(time.Now().UTC().UnixNano())
}
func GetRandomName() string {
result := []string{}
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, mathrand.Uint32())
result = mnemonicode.EncodeWordList(result, bs)
return strings.Join(result, "-")
}
2017-10-18 16:05:48 +03:00
func Encrypt(plaintext []byte, passphrase string, dontencrypt ...bool) (encrypted []byte, salt string, iv string) {
if len(dontencrypt) > 0 && dontencrypt[0] {
2017-10-18 07:23:31 +03:00
return plaintext, "salt", "iv"
}
2017-10-18 16:05:48 +03:00
key, saltBytes := deriveKey(passphrase, nil)
ivBytes := make([]byte, 12)
2017-10-18 07:23:31 +03:00
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// Section 8.2
2017-10-18 16:05:48 +03:00
rand.Read(ivBytes)
2017-10-18 07:23:31 +03:00
b, _ := aes.NewCipher(key)
aesgcm, _ := cipher.NewGCM(b)
2017-10-18 16:05:48 +03:00
encrypted = aesgcm.Seal(nil, ivBytes, plaintext, nil)
salt = hex.EncodeToString(saltBytes)
iv = hex.EncodeToString(ivBytes)
return
}
2017-10-18 16:05:48 +03:00
func Decrypt(data []byte, passphrase string, salt string, iv string, dontencrypt ...bool) (plaintext []byte, err error) {
if len(dontencrypt) > 0 && dontencrypt[0] {
2017-10-18 07:23:31 +03:00
return data, nil
}
saltBytes, _ := hex.DecodeString(salt)
ivBytes, _ := hex.DecodeString(iv)
key, _ := deriveKey(passphrase, saltBytes)
b, _ := aes.NewCipher(key)
aesgcm, _ := cipher.NewGCM(b)
plaintext, err = aesgcm.Open(nil, ivBytes, data, nil)
return
2017-10-18 05:41:52 +03:00
}
2017-10-18 05:41:52 +03:00
func deriveKey(passphrase string, salt []byte) ([]byte, []byte) {
if salt == nil {
salt = make([]byte, 8)
// http://www.ietf.org/rfc/rfc2898.txt
// Salt.
rand.Read(salt)
}
2017-10-18 05:41:52 +03:00
return pbkdf2.Key([]byte(passphrase), salt, 1000, 32, sha256.New), salt
}
func Hash(data string) string {
2017-10-18 06:28:32 +03:00
return HashBytes([]byte(data))
}
func HashBytes(data []byte) string {
sum := sha256.Sum256(data)
2017-10-18 05:33:27 +03:00
return fmt.Sprintf("%x", sum)
}
2017-10-20 04:01:07 +03:00
func EncryptFile(inputFilename string, outputFilename string, password string) error {
return cryptFile(inputFilename, outputFilename, password, true)
}
func DecryptFile(inputFilename string, outputFilename string, password string) error {
return cryptFile(inputFilename, outputFilename, password, false)
}
func cryptFile(inputFilename string, outputFilename string, password string, encrypt bool) error {
in, err := os.Open(inputFilename)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(outputFilename)
if err != nil {
return err
}
defer func() {
if err := out.Sync(); err != nil {
log.Error(err)
}
if err := out.Close(); err != nil {
log.Error(err)
}
}()
c := &crypt.Crypter{
HashFunc: sha1.New,
HashSize: sha1.Size,
Key: crypt.NewPbkdf2Key([]byte(password), 32),
}
if encrypt {
if err := c.Encrypt(out, in); err != nil {
return err
}
} else {
if err := c.Decrypt(out, in); err != nil {
return err
}
}
return nil
}