The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/internal/blsgen/lib.go

142 lines
3.6 KiB

6 years ago
package blsgen
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
6 years ago
"os"
6 years ago
"time"
ffi_bls "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto/bls"
)
func toISO8601(t time.Time) string {
var tz string
name, offset := t.Zone()
if name == "UTC" {
tz = "Z"
} else {
tz = fmt.Sprintf("%03d00", offset/3600)
}
return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s",
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
}
func keyFileName(publicKey *ffi_bls.PublicKey) string {
ts := time.Now().UTC()
serializedPublicKey := publicKey.SerializeToHexStr()
return fmt.Sprintf("UTC--%s--bls_%s", toISO8601(ts), serializedPublicKey)
6 years ago
}
// WritePrivateKeyWithPassPhrase encrypt the key with passphrase and write into disk.
func WritePrivateKeyWithPassPhrase(privateKey *ffi_bls.SecretKey, passphrase string) string {
publickKey := privateKey.GetPublicKey()
fileName := keyFileName(publickKey)
privateKeyHex := privateKey.SerializeToHexStr()
// Encrypt with passphrase
encryptedPrivateKeyBytes := encrypt([]byte(privateKeyHex), passphrase)
// Write to file.
err := ioutil.WriteFile(fileName, encryptedPrivateKeyBytes, 0600)
check(err)
return fileName
}
6 years ago
// GenBlsKeyWithPassPhrase generates bls key with passphrase and write into disk.
func GenBlsKeyWithPassPhrase(passphrase string) (*ffi_bls.SecretKey, string) {
privateKey := bls.RandPrivateKey()
publickKey := privateKey.GetPublicKey()
fileName := keyFileName(publickKey)
privateKeyHex := privateKey.SerializeToHexStr()
6 years ago
// Encrypt with passphrase
6 years ago
encryptedPrivateKeyStr := encrypt([]byte(privateKeyHex), passphrase)
6 years ago
// Write to file.
6 years ago
err := WriteToFile(fileName, encryptedPrivateKeyStr)
6 years ago
check(err)
return privateKey, fileName
}
6 years ago
// WriteToFile will print any string of text to a file safely by
// checking for errors and syncing at the end.
func WriteToFile(filename string, data string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = io.WriteString(file, data)
if err != nil {
return err
}
return file.Sync()
}
6 years ago
// LoadBlsKeyWithPassPhrase loads bls key with passphrase.
func LoadBlsKeyWithPassPhrase(fileName, passphrase string) *ffi_bls.SecretKey {
encryptedPrivateKeyBytes, err := ioutil.ReadFile(fileName)
check(err)
decryptedBytes := decrypt(encryptedPrivateKeyBytes, passphrase)
priKey := &ffi_bls.SecretKey{}
priKey.DeserializeHexStr(string(decryptedBytes))
6 years ago
return priKey
}
func createHash(key string) string {
hasher := md5.New()
hasher.Write([]byte(key))
return hex.EncodeToString(hasher.Sum(nil))
}
6 years ago
func encrypt(data []byte, passphrase string) string {
6 years ago
block, _ := aes.NewCipher([]byte(createHash(passphrase)))
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
6 years ago
return hex.EncodeToString(ciphertext)
6 years ago
}
6 years ago
func decrypt(encryptedStr string, passphrase string) []byte {
var err error
6 years ago
key := []byte(createHash(passphrase))
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
6 years ago
data, err := hex.DecodeString(encryptedStr)
if err != nil {
panic(err.Error())
}
6 years ago
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
panic(err.Error())
}
return plaintext
}
func check(err error) {
// handle this error
if err != nil {
fmt.Println(err)
}
}