security: optimize alg2 function for R=4

This commit is contained in:
Denys Smirnov 2018-11-05 23:37:45 +02:00
parent 7521c7cee4
commit 7f8c6b65ae

View File

@ -10,6 +10,7 @@ import (
"crypto/md5"
"crypto/rand"
"crypto/rc4"
"encoding/binary"
"errors"
"github.com/unidoc/unidoc/common"
@ -56,12 +57,9 @@ func (sh stdHandlerR4) alg2(d *StdEncryptDict, pass []byte) []byte {
h.Write(d.O)
// Pass P (Lower order byte first).
var p = uint32(d.P)
var pb []byte
for i := 0; i < 4; i++ {
pb = append(pb, byte(((p >> uint(8*i)) & 0xff)))
}
h.Write(pb)
var pb [4]byte
binary.LittleEndian.PutUint32(pb[:], uint32(d.P))
h.Write(pb[:])
common.Log.Trace("go P: % x", pb)
// Pass ID[0] from the trailer
@ -74,8 +72,9 @@ func (sh stdHandlerR4) alg2(d *StdEncryptDict, pass []byte) []byte {
hashb := h.Sum(nil)
if d.R >= 3 {
h = md5.New()
for i := 0; i < 50; i++ {
h = md5.New()
h.Reset()
h.Write(hashb[0 : sh.Length/8])
hashb = h.Sum(nil)
}