Skip to content

Commit

Permalink
performance optimization for extract encrypted 7z
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroyukki committed Mar 29, 2024
1 parent 31f77ef commit ea42a59
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/aes7z/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,50 @@ import (
"bytes"
"crypto/sha256"
"encoding/binary"
"sync"

"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)

type keyCacheItem struct {
password string
cycles int
salt []byte
key []byte
}

func (c *keyCacheItem) hittest(password string, cycles int, salt []byte) bool {
return c.password == password && c.cycles == cycles && bytes.Equal(salt, c.salt)
}

var keyCache []*keyCacheItem = []*keyCacheItem{}
var keyCacheLock sync.RWMutex

func findKeyCached(password string, cycles int, salt []byte) []byte {
keyCacheLock.RLock()
defer keyCacheLock.RUnlock()
for _, kci := range keyCache {
if kci.hittest(password, cycles, salt) {
return kci.key
}
}

return nil
}

func recordKeyCached(password string, cycles int, salt []byte, key []byte) {
keyCacheLock.Lock()
defer keyCacheLock.Unlock()
keyCache = append(keyCache, &keyCacheItem{password: password, cycles: cycles, salt: salt, key: key})
}

func calculateKey(password string, cycles int, salt []byte) []byte {
k := findKeyCached(password, cycles, salt)
if len(k) > 0 {
// key found in cache
return k
}
b := bytes.NewBuffer(salt)

// Convert password to UTF-16LE
Expand All @@ -30,5 +68,6 @@ func calculateKey(password string, cycles int, salt []byte) []byte {
copy(key, h.Sum(nil))
}

recordKeyCached(password, cycles, salt, key)
return key
}

0 comments on commit ea42a59

Please sign in to comment.