diff --git a/openpgp/packet/opaque.go b/openpgp/packet/opaque.go index 7c4d813..5f9ad44 100644 --- a/openpgp/packet/opaque.go +++ b/openpgp/packet/opaque.go @@ -7,7 +7,6 @@ package packet import ( "bytes" "code.google.com/p/go.crypto/openpgp/errors" - "encoding/binary" "io" "io/ioutil" ) @@ -152,11 +151,9 @@ Truncated: func (osp *OpaqueSubpacket) Serialize(w io.Writer) (err error) { buf := make([]byte, 6) - buf[0] = 0xff - // Header length includes the subtype byte - binary.BigEndian.PutUint32(buf[1:5], uint32(len(osp.Contents)+1)) - buf[5] = osp.SubType - if _, err = w.Write(buf); err != nil { + n := serializeSubpacketLength(buf, len(osp.Contents)+1) + buf[n] = osp.SubType + if _, err = w.Write(buf[:n+1]); err != nil { return } _, err = w.Write(osp.Contents) diff --git a/openpgp/packet/signature.go b/openpgp/packet/signature.go index d0d6890..dbed36e 100644 --- a/openpgp/packet/signature.go +++ b/openpgp/packet/signature.go @@ -344,13 +344,14 @@ func subpacketLengthLength(length int) int { // serializeSubpacketLength marshals the given length into to. func serializeSubpacketLength(to []byte, length int) int { + // RFC 4880, Section 4.2.2. if length < 192 { to[0] = byte(length) return 1 } if length < 16320 { length -= 192 - to[0] = byte(length >> 8) + to[0] = byte((length >> 8) + 192) to[1] = byte(length) return 2 }