Remove EncryptFrom*() methods, add Tests for EncryptTo()

This commit is contained in:
Alexander Neumann
2015-02-15 23:38:45 +01:00
parent 5c724b92b8
commit 3dbe02182b
2 changed files with 19 additions and 52 deletions

42
key.go
View File

@@ -334,48 +334,6 @@ func (k *Key) Encrypt(ciphertext, plaintext []byte) (int, error) {
return k.encrypt(k.master, ciphertext, plaintext)
}
// encryptFrom encrypts and signs data read from rd with ks. The returned
// io.Reader reads IV || Ciphertext || HMAC. For the hash function, SHA256 is
// used.
func (k *Key) encryptFrom(ks *keys, rd io.Reader) io.Reader {
// create IV
iv := make([]byte, ivSize)
_, err := io.ReadFull(rand.Reader, iv)
if err != nil {
panic(fmt.Sprintf("unable to generate new random iv: %v", err))
}
c, err := aes.NewCipher(ks.Encrypt)
if err != nil {
panic(fmt.Sprintf("unable to create cipher: %v", err))
}
ivReader := bytes.NewReader(iv)
encryptReader := cipher.StreamReader{
R: rd,
S: cipher.NewCTR(c, iv),
}
return backend.NewHashAppendReader(io.MultiReader(ivReader, encryptReader),
hmac.New(sha256.New, ks.Sign))
}
// EncryptFrom encrypts and signs data read from rd with the master key. The
// returned io.Reader reads IV || Ciphertext || HMAC. For the hash function,
// SHA256 is used.
func (k *Key) EncryptFrom(rd io.Reader) io.Reader {
return k.encryptFrom(k.master, rd)
}
// EncryptFrom encrypts and signs data read from rd with the user key. The
// returned io.Reader reads IV || Ciphertext || HMAC. For the hash function,
// SHA256 is used.
func (k *Key) EncryptUserFrom(rd io.Reader) io.Reader {
return k.encryptFrom(k.user, rd)
}
type encryptWriter struct {
iv []byte
wroteIV bool