Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Oct 8, 2024
1 parent 7c03223 commit a931880
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
16 changes: 8 additions & 8 deletions kdf/pgp_s2k/pgp_s2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ func Key(h func() hash.Hash, password, salt []byte, iterations, keylen int) []by
var generated int = 0

for generated < keylen {
output_this_pass := mathMin(newHash.Size(), keylen - generated)
outputThisPass := mathMin(newHash.Size(), keylen - generated)

newHash.Reset()

// Preload some number of zero bytes (empty first iteration)
zero_padding := bytes.Repeat([]byte{0}, pass)
newHash.Write(zero_padding)
zeroPadding := bytes.Repeat([]byte{0}, pass)
newHash.Write(zeroPadding)

// The input is always fully processed even if iterations is very small
if len(inputBuf) > 0 {
left := mathMax(iterations, len(inputBuf))
for left > 0 {
input_to_take := mathMin(left, len(inputBuf))
newHash.Write(inputBuf[:input_to_take])
left -= input_to_take
input2Take := mathMin(left, len(inputBuf))
newHash.Write(inputBuf[:input2Take])
left -= input2Take
}
}

hashBuf = newHash.Sum(nil)

outputBuf = append(outputBuf, hashBuf[:output_this_pass]...)
outputBuf = append(outputBuf, hashBuf[:outputThisPass]...)

generated += output_this_pass
generated += outputThisPass
pass++
}

Expand Down
34 changes: 17 additions & 17 deletions kdf/pgp_s2k/s2k_family.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,48 @@ import (
)

func tune(h hash.Hash, keylen int, msec time.Duration, _ int, tune_time time.Duration) int {
var buf_size int = 1024
var buffer = make([]byte, buf_size)
var time_used uint64 = 0
var event_count uint64 = 0
var bufSize int = 1024
var buffer = make([]byte, bufSize)
var timeUsed uint64 = 0
var eventCount uint64 = 0

td := time.Duration(buf_size)
td := time.Duration(bufSize)

timer := time.NewTimer(td)
for {
select {
case <-timer.C:
event_count++
time_used = time_used + uint64(td)
eventCount++
timeUsed = timeUsed + uint64(td)

h.Write(buffer)

if time.Duration(time_used) < tune_time {
if time.Duration(timeUsed) < tune_time {
timer.Reset(td)
}
}
}

var hash_bytes_per_second uint64
var hashBytesPerSecond uint64
if td.Seconds() > 0 {
hash_bytes_per_second = (uint64(buf_size) * event_count) / uint64(td.Seconds())
hashBytesPerSecond = (uint64(bufSize) * eventCount) / uint64(td.Seconds())
} else {
hash_bytes_per_second = 0
hashBytesPerSecond = 0
}

desired_nsec := uint64(msec.Nanoseconds())
desiredNsec := uint64(msec.Nanoseconds())

hash_size := h.Size()
hashSize := h.Size()

var blocks_required int
if keylen <= hash_size {
if keylen <= hashSize {
blocks_required = 1
} else {
blocks_required = (keylen + hash_size - 1) / hash_size
blocks_required = (keylen + hashSize - 1) / hashSize
}

bytes_to_be_hashed := (hash_bytes_per_second * (desired_nsec / 1000000000)) / uint64(blocks_required)
iterations := roundIterations(uint32(bytes_to_be_hashed))
bytesToBeHashed := (hashBytesPerSecond * (desiredNsec / 1000000000)) / uint64(blocks_required)
iterations := roundIterations(uint32(bytesToBeHashed))

return int(iterations)
}

0 comments on commit a931880

Please sign in to comment.