1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "golang.org/x/crypto/openpgp/errors"
11 "golang.org/x/crypto/openpgp/s2k"
16 // OnePassSignature represents a one-pass signature packet. See RFC 4880,
18 type OnePassSignature struct {
21 PubKeyAlgo PublicKeyAlgorithm
26 const onePassSignatureVersion = 3
28 func (ops *OnePassSignature) parse(r io.Reader) (err error) {
31 _, err = readFull(r, buf[:])
35 if buf[0] != onePassSignatureVersion {
36 err = errors.UnsupportedError("one-pass-signature packet version " + strconv.Itoa(int(buf[0])))
40 ops.Hash, ok = s2k.HashIdToHash(buf[2])
42 return errors.UnsupportedError("hash function: " + strconv.Itoa(int(buf[2])))
45 ops.SigType = SignatureType(buf[1])
46 ops.PubKeyAlgo = PublicKeyAlgorithm(buf[3])
47 ops.KeyId = binary.BigEndian.Uint64(buf[4:12])
48 ops.IsLast = buf[12] != 0
52 // Serialize marshals the given OnePassSignature to w.
53 func (ops *OnePassSignature) Serialize(w io.Writer) error {
55 buf[0] = onePassSignatureVersion
56 buf[1] = uint8(ops.SigType)
58 buf[2], ok = s2k.HashToHashId(ops.Hash)
60 return errors.UnsupportedError("hash type: " + strconv.Itoa(int(ops.Hash)))
62 buf[3] = uint8(ops.PubKeyAlgo)
63 binary.BigEndian.PutUint64(buf[4:12], ops.KeyId)
68 if err := serializeHeader(w, packetTypeOnePassSignature, len(buf)); err != nil {
71 _, err := w.Write(buf[:])