Source File
write.go
Belonging Package
golang.org/x/crypto/openpgp
package openpgp
import (
)
func ( io.Writer, *Entity, io.Reader, *packet.Config) error {
return armoredDetachSign(, , , packet.SigTypeText, )
}
func ( io.Writer, *Entity, io.Reader, packet.SignatureType, *packet.Config) ( error) {
, := armor.Encode(, SignatureType, nil)
if != nil {
return
}
= detachSign(, , , , )
if != nil {
return
}
return .Close()
}
func ( io.Writer, *Entity, io.Reader, packet.SignatureType, *packet.Config) ( error) {
if .PrivateKey == nil {
return errors.InvalidArgumentError("signing key doesn't have a private key")
}
if .PrivateKey.Encrypted {
return errors.InvalidArgumentError("signing key is encrypted")
}
:= new(packet.Signature)
.SigType =
.PubKeyAlgo = .PrivateKey.PubKeyAlgo
.Hash = .Hash()
.CreationTime = .Now()
.IssuerKeyId = &.PrivateKey.KeyId
, , := hashForSignature(.Hash, .SigType)
if != nil {
return
}
io.Copy(, )
= .Sign(, .PrivateKey, )
if != nil {
return
}
return .Serialize()
}
func ( io.Writer, []byte, *FileHints, *packet.Config) ( io.WriteCloser, error) {
if == nil {
= &FileHints{}
}
, := packet.SerializeSymmetricKeyEncrypted(, , )
if != nil {
return
}
, := packet.SerializeSymmetricallyEncrypted(, .Cipher(), , )
if != nil {
return
}
:=
if := .Compression(); != packet.CompressionNone {
var *packet.CompressionConfig
if != nil {
= .CompressionConfig
}
, = packet.SerializeCompressed(, , )
if != nil {
return
}
}
var uint32
if !.ModTime.IsZero() {
= uint32(.ModTime.Unix())
}
return packet.SerializeLiteral(, .IsBinary, .FileName, )
}
func ( io.WriteCloser, []uint8, *Entity, *FileHints, *packet.Config) ( io.WriteCloser, error) {
var *packet.PrivateKey
if != nil {
, := .signingKey(.Now())
if ! {
return nil, errors.InvalidArgumentError("no valid signing keys")
}
= .PrivateKey
if == nil {
return nil, errors.InvalidArgumentError("no private key in signing key")
}
if .Encrypted {
return nil, errors.InvalidArgumentError("signing key must be decrypted")
}
}
var crypto.Hash
for , := range {
if , := s2k.HashIdToHash(); && .Available() {
=
break
}
}
if := .Hash(); .Available() {
for , := range {
if , := s2k.HashIdToHash(); && == {
=
break
}
}
}
if == 0 {
:= [0]
, := s2k.HashIdToString()
if ! {
= "#" + strconv.Itoa(int())
}
return nil, errors.InvalidArgumentError("cannot encrypt because no candidate hash functions are compiled in. (Wanted " + + " in this case.)")
}
if != nil {
:= &packet.OnePassSignature{
SigType: packet.SigTypeBinary,
Hash: ,
PubKeyAlgo: .PubKeyAlgo,
KeyId: .KeyId,
IsLast: true,
}
if := .Serialize(); != nil {
return nil,
}
}
if == nil {
= &FileHints{}
}
:=
:= []uint8{
uint8(packet.CipherAES128),
uint8(packet.CipherAES256),
uint8(packet.CipherCAST5),
:= [len()-1:]
:= [len()-1:]
:= make([]Key, len())
for := range {
var bool
[], = [].encryptionKey(.Now())
if ! {
return nil, errors.InvalidArgumentError("cannot encrypt a message to key id " + strconv.FormatUint([].PrimaryKey.KeyId, 16) + " because it has no encryption keys")
}
:= [].primaryIdentity().SelfSignature
:= .PreferredSymmetric
if len() == 0 {
=
}
:= .PreferredHash
if len() == 0 {
=
}
= intersectPreferences(, )
= intersectPreferences(, )
}
if len() == 0 || len() == 0 {
return nil, errors.InvalidArgumentError("cannot encrypt because recipient set shares no common algorithms")
}
:= .Cipher()
for , := range {
:= packet.CipherFunction()
if == {
=
break
}
}
:= make([]byte, .KeySize())
if , := io.ReadFull(.Random(), ); != nil {
return nil,
}
for , := range {
if := packet.SerializeEncryptedKey(, .PublicKey, , , ); != nil {
return nil,
}
}
, := packet.SerializeSymmetricallyEncrypted(, , , )
if != nil {
return
}
return writeAndSign(, , , , )
}
:= []uint8{
hashToHashId(crypto.SHA256),
hashToHashId(crypto.SHA384),
hashToHashId(crypto.SHA512),
hashToHashId(crypto.SHA1),
hashToHashId(crypto.RIPEMD160),
}
:= [len()-1:]
:= .primaryIdentity().SelfSignature.PreferredHash
if len() == 0 {
=
}
= intersectPreferences(, )
return writeAndSign(noOpCloser{}, , , , )
}
type signatureWriter struct {
encryptedData io.WriteCloser
literalData io.WriteCloser
hashType crypto.Hash
h hash.Hash
signer *packet.PrivateKey
config *packet.Config
}
func ( signatureWriter) ( []byte) (int, error) {
.h.Write()
return .literalData.Write()
}
func ( signatureWriter) () error {
:= &packet.Signature{
SigType: packet.SigTypeBinary,
PubKeyAlgo: .signer.PubKeyAlgo,
Hash: .hashType,
CreationTime: .config.Now(),
IssuerKeyId: &.signer.KeyId,
}
if := .Sign(.h, .signer, .config); != nil {
return
}
if := .literalData.Close(); != nil {
return
}
if := .Serialize(.encryptedData); != nil {
return
}
return .encryptedData.Close()
}
type noOpCloser struct {
w io.Writer
}
func ( noOpCloser) ( []byte) ( int, error) {
return .w.Write()
}
func ( noOpCloser) () error {
return nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |