2017-10-18 05:19:09 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2017-10-18 05:33:27 +03:00
|
|
|
"crypto/sha256"
|
2017-10-18 05:19:09 +03:00
|
|
|
"encoding/binary"
|
2017-10-18 05:33:27 +03:00
|
|
|
"fmt"
|
2017-10-18 05:19:09 +03:00
|
|
|
mathrand "math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/schollz/mnemonicode"
|
2017-10-18 05:41:52 +03:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2017-10-18 05:19:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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 06:15:48 +03:00
|
|
|
func Encrypt(plaintext []byte, passphrase string) ([]byte, string, string) {
|
2017-10-18 07:03:48 +03:00
|
|
|
return plaintext, "salt", "iv"
|
|
|
|
// key, salt := deriveKey(passphrase, nil)
|
|
|
|
// iv := make([]byte, 12)
|
|
|
|
// // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
|
|
|
// // Section 8.2
|
|
|
|
// rand.Read(iv)
|
|
|
|
// b, _ := aes.NewCipher(key)
|
|
|
|
// aesgcm, _ := cipher.NewGCM(b)
|
|
|
|
// data := aesgcm.Seal(nil, iv, plaintext, nil)
|
|
|
|
// return data, hex.EncodeToString(salt), hex.EncodeToString(iv)
|
2017-10-18 05:19:09 +03:00
|
|
|
}
|
|
|
|
|
2017-10-18 06:15:48 +03:00
|
|
|
func Decrypt(data []byte, passphrase string, salt string, iv string) (plaintext []byte, err error) {
|
2017-10-18 07:12:35 +03:00
|
|
|
return data, nil
|
2017-10-18 07:03:48 +03:00
|
|
|
// 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:19:09 +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:19:09 +03:00
|
|
|
}
|
2017-10-18 05:41:52 +03:00
|
|
|
return pbkdf2.Key([]byte(passphrase), salt, 1000, 32, sha256.New), salt
|
2017-10-18 05:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-18 05:19:09 +03:00
|
|
|
}
|