Exemple #1
0
func (mk *MetaKey) Export(password, filename string) (err error) {
	buf := new(bytes.Buffer)
	err = mk.Write(buf)
	if err != nil {
		return
	}
	pk := hash.DeriveKey(password)
	out := hash.New(buf.Bytes()).Digest()
	out = append(out, buf.Bytes()...)
	enc, err := armour.Encrypt(binaryKeyFromDerivedKey(pk), out, false)
	if err != nil {
		return
	}

	out = pk.Salt
	out = append(out, enc...)
	err = ioutil.WriteFile(filename, armour.EncodeBase64(out), 0644)
	return
}
Exemple #2
0
func (mk *MetaKey) Encrypt(algo int, msg []byte, armoured bool) (ct []byte, err error) {
	var keySize int
	switch algo {
	case AES128:
		keySize = AES128KeyLength
	case AES192:
		keySize = AES192KeyLength
	case AES256:
		keySize = AES256KeyLength
	default:
		return ct, ErrKeySizeNotSupported
	}

	key, err := mk.GetKey(keySize)
	if err != nil {
		return
	}

	return armour.Encrypt(key, msg, armoured)
}