...

Source file src/golang.org/x/crypto/openpgp/packet/userattribute.go

Documentation: golang.org/x/crypto/openpgp/packet

     1  // Copyright 2013 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.
     4  
     5  package packet
     6  
     7  import (
     8  	"bytes"
     9  	"image"
    10  	"image/jpeg"
    11  	"io"
    12  )
    13  
    14  const UserAttrImageSubpacket = 1
    15  
    16  // UserAttribute is capable of storing other types of data about a user
    17  // beyond name, email and a text comment. In practice, user attributes are typically used
    18  // to store a signed thumbnail photo JPEG image of the user.
    19  // See RFC 4880, section 5.12.
    20  type UserAttribute struct {
    21  	Contents []*OpaqueSubpacket
    22  }
    23  
    24  // NewUserAttributePhoto creates a user attribute packet
    25  // containing the given images.
    26  func NewUserAttributePhoto(photos ...image.Image) (uat *UserAttribute, err error) {
    27  	uat = new(UserAttribute)
    28  	for _, photo := range photos {
    29  		var buf bytes.Buffer
    30  		// RFC 4880, Section 5.12.1.
    31  		data := []byte{
    32  			0x10, 0x00, // Little-endian image header length (16 bytes)
    33  			0x01,       // Image header version 1
    34  			0x01,       // JPEG
    35  			0, 0, 0, 0, // 12 reserved octets, must be all zero.
    36  			0, 0, 0, 0,
    37  			0, 0, 0, 0}
    38  		if _, err = buf.Write(data); err != nil {
    39  			return
    40  		}
    41  		if err = jpeg.Encode(&buf, photo, nil); err != nil {
    42  			return
    43  		}
    44  		uat.Contents = append(uat.Contents, &OpaqueSubpacket{
    45  			SubType:  UserAttrImageSubpacket,
    46  			Contents: buf.Bytes()})
    47  	}
    48  	return
    49  }
    50  
    51  // NewUserAttribute creates a new user attribute packet containing the given subpackets.
    52  func NewUserAttribute(contents ...*OpaqueSubpacket) *UserAttribute {
    53  	return &UserAttribute{Contents: contents}
    54  }
    55  
    56  func (uat *UserAttribute) parse(r io.Reader) (err error) {
    57  	// RFC 4880, section 5.13
    58  	b, err := io.ReadAll(r)
    59  	if err != nil {
    60  		return
    61  	}
    62  	uat.Contents, err = OpaqueSubpackets(b)
    63  	return
    64  }
    65  
    66  // Serialize marshals the user attribute to w in the form of an OpenPGP packet, including
    67  // header.
    68  func (uat *UserAttribute) Serialize(w io.Writer) (err error) {
    69  	var buf bytes.Buffer
    70  	for _, sp := range uat.Contents {
    71  		sp.Serialize(&buf)
    72  	}
    73  	if err = serializeHeader(w, packetTypeUserAttribute, buf.Len()); err != nil {
    74  		return err
    75  	}
    76  	_, err = w.Write(buf.Bytes())
    77  	return
    78  }
    79  
    80  // ImageData returns zero or more byte slices, each containing
    81  // JPEG File Interchange Format (JFIF), for each photo in the
    82  // user attribute packet.
    83  func (uat *UserAttribute) ImageData() (imageData [][]byte) {
    84  	for _, sp := range uat.Contents {
    85  		if sp.SubType == UserAttrImageSubpacket && len(sp.Contents) > 16 {
    86  			imageData = append(imageData, sp.Contents[16:])
    87  		}
    88  	}
    89  	return
    90  }
    91  

View as plain text