Skip to content

Commit

Permalink
to be merged: cache cipher in TransparentEncryptionInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
urykhy committed Mar 6, 2022
1 parent 7398261 commit 00a4963
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 8 additions & 4 deletions aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ func aesCtrStep(offset int64, enc *TransparentEncryptionInfo, b []byte) ([]byte,
return nil, err
}

block, err := aes.NewCipher(enc.key)
if err != nil {
return nil, err
if enc.cipher == nil {
cipher, err := aes.NewCipher(enc.key)
if err != nil {
return nil, err
}
enc.cipher = cipher
}
stream := cipher.NewCTR(block, iv)

stream := cipher.NewCTR(enc.cipher, iv)

padding := offset % aes.BlockSize
if padding > 0 {
Expand Down
6 changes: 4 additions & 2 deletions file_reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hdfs

import (
"crypto/cipher"
"crypto/md5"
"errors"
"fmt"
Expand Down Expand Up @@ -36,8 +37,9 @@ type FileReader struct {

// A TransparentEncryptionInfo is a key and iv to encrypt or decrypt file data
type TransparentEncryptionInfo struct {
key []byte
iv []byte
key []byte
iv []byte
cipher cipher.Block
}

// Open returns an FileReader which can be used for reading.
Expand Down

0 comments on commit 00a4963

Please sign in to comment.