diff --git a/pdf/core/security/standard_r4.go b/pdf/core/security/standard_r4.go index 5565bfc1..9efc18bf 100644 --- a/pdf/core/security/standard_r4.go +++ b/pdf/core/security/standard_r4.go @@ -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) }