Source File
packet.go
Belonging Package
golang.org/x/crypto/openpgp/packet
package packet // import "golang.org/x/crypto/openpgp/packet"
import (
)
func ( io.Reader) ( int64, bool, error) {
var [4]byte
_, = readFull(, [:1])
if != nil {
return
}
switch {
case [0] < 192:
= int64([0])
case [0] < 224:
= int64([0]-192) << 8
_, = readFull(, [0:1])
if != nil {
return
}
+= int64([0]) + 192
case [0] < 255:
= int64(1) << ([0] & 0x1f)
= true
default:
_, = readFull(, [0:4])
if != nil {
return
}
= int64([0])<<24 |
int64([1])<<16 |
int64([2])<<8 |
int64([3])
}
return
}
type partialLengthReader struct {
r io.Reader
remaining int64
isPartial bool
}
func ( *partialLengthReader) ( []byte) ( int, error) {
for .remaining == 0 {
if !.isPartial {
return 0, io.EOF
}
.remaining, .isPartial, = readLength(.r)
if != nil {
return 0,
}
}
:= int64(len())
if > .remaining {
= .remaining
}
, = .r.Read([:int()])
.remaining -= int64()
if < int() && == io.EOF {
= io.ErrUnexpectedEOF
}
return
}
type partialLengthWriter struct {
w io.WriteCloser
lengthByte [1]byte
sentFirst bool
buf []byte
}
const minFirstPartialWrite = 512
func ( *partialLengthWriter) ( []byte) ( int, error) {
:= 0
if !.sentFirst {
if len(.buf) > 0 || len() < minFirstPartialWrite {
= len(.buf)
.buf = append(.buf, ...)
if len(.buf) < minFirstPartialWrite {
return len(), nil
}
= .buf
.buf = nil
}
.sentFirst = true
}
:= uint8(30)
for len() > 0 {
:= 1 <<
if len() < {
= uint8(bits.Len32(uint32(len()))) - 1
= 1 <<
}
.lengthByte[0] = 224 +
_, = .w.Write(.lengthByte[:])
if == nil {
var int
, = .w.Write([:])
+=
}
if != nil {
if < {
return 0,
}
return - ,
}
= [:]
}
return - , nil
}
func ( *partialLengthWriter) () error {
= packetType(([0] & 0x3f) >> 2)
:= [0] & 3
if == 3 {
= -1
=
return
}
:= 1 <<
_, = readFull(, [0:])
if != nil {
return
}
for := 0; < ; ++ {
<<= 8
|= int64([])
}
= &spanReader{, }
return
}
= packetType([0] & 0x3f)
, , := readLength()
if != nil {
return
}
if {
= &partialLengthReader{
remaining: ,
isPartial: true,
r: ,
}
= -1
} else {
= &spanReader{, }
}
return
}
func ( io.Writer, packetType, int) ( error) {
var [6]byte
var int
[0] = 0x80 | 0x40 | byte()
if < 192 {
[1] = byte()
= 2
} else if < 8384 {
-= 192
[1] = 192 + byte(>>8)
[2] = byte()
= 3
} else {
[1] = 255
[2] = byte( >> 24)
[3] = byte( >> 16)
[4] = byte( >> 8)
[5] = byte()
= 6
}
_, = .Write([:])
return
}
func ( io.WriteCloser, packetType) ( io.WriteCloser, error) {
var [1]byte
[0] = 0x80 | 0x40 | byte()
_, = .Write([:])
if != nil {
return
}
= &partialLengthWriter{w: }
return
}
type packetType uint8
const (
packetTypeEncryptedKey packetType = 1
packetTypeSignature packetType = 2
packetTypeSymmetricKeyEncrypted packetType = 3
packetTypeOnePassSignature packetType = 4
packetTypePrivateKey packetType = 5
packetTypePublicKey packetType = 6
packetTypePrivateSubkey packetType = 7
packetTypeCompressed packetType = 8
packetTypeSymmetricallyEncrypted packetType = 9
packetTypeLiteralData packetType = 11
packetTypeUserId packetType = 13
packetTypePublicSubkey packetType = 14
packetTypeUserAttribute packetType = 17
packetTypeSymmetricallyEncryptedMDC packetType = 18
)
func ( io.Reader) ( Packet, error) {
, , , := readHeader()
if != nil {
return
}
switch {
case packetTypeEncryptedKey:
= new(EncryptedKey)
case packetTypeSignature:
if , , = peekVersion(); != nil {
return
}
if < 4 {
= new(SignatureV3)
} else {
= new(Signature)
}
case packetTypeSymmetricKeyEncrypted:
= new(SymmetricKeyEncrypted)
case packetTypeOnePassSignature:
= new(OnePassSignature)
case packetTypePrivateKey, packetTypePrivateSubkey:
:= new(PrivateKey)
if == packetTypePrivateSubkey {
.IsSubkey = true
}
=
case packetTypePublicKey, packetTypePublicSubkey:
var byte
if , , = peekVersion(); != nil {
return
}
:= == packetTypePublicSubkey
if < 4 {
= &PublicKeyV3{IsSubkey: }
} else {
= &PublicKey{IsSubkey: }
}
case packetTypeCompressed:
= new(Compressed)
case packetTypeSymmetricallyEncrypted:
= new(SymmetricallyEncrypted)
case packetTypeLiteralData:
= new(LiteralData)
case packetTypeUserId:
= new(UserId)
case packetTypeUserAttribute:
= new(UserAttribute)
case packetTypeSymmetricallyEncryptedMDC:
:= new(SymmetricallyEncrypted)
.MDC = true
=
default:
= errors.UnknownPacketTypeError()
}
if != nil {
= .parse()
}
if != nil {
consumeAll()
}
return
}
type SignatureType uint8
const (
SigTypeBinary SignatureType = 0
SigTypeText = 1
SigTypeGenericCert = 0x10
SigTypePersonaCert = 0x11
SigTypeCasualCert = 0x12
SigTypePositiveCert = 0x13
SigTypeSubkeyBinding = 0x18
SigTypePrimaryKeyBinding = 0x19
SigTypeDirectSignature = 0x1F
SigTypeKeyRevocation = 0x20
SigTypeSubkeyRevocation = 0x28
)
type PublicKeyAlgorithm uint8
const (
PubKeyAlgoRSA PublicKeyAlgorithm = 1
PubKeyAlgoElGamal PublicKeyAlgorithm = 16
func ( PublicKeyAlgorithm) () bool {
switch {
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly, PubKeyAlgoElGamal:
return true
}
return false
}
func ( PublicKeyAlgorithm) () bool {
switch {
case PubKeyAlgoRSA, PubKeyAlgoRSASignOnly, PubKeyAlgoDSA, PubKeyAlgoECDSA:
return true
}
return false
}
type CipherFunction uint8
const (
Cipher3DES CipherFunction = 2
CipherCAST5 CipherFunction = 3
CipherAES128 CipherFunction = 7
CipherAES192 CipherFunction = 8
CipherAES256 CipherFunction = 9
)
func ( CipherFunction) () int {
switch {
case Cipher3DES:
return 24
case CipherCAST5:
return cast5.KeySize
case CipherAES128:
return 16
case CipherAES192:
return 24
case CipherAES256:
return 32
}
return 0
}
func ( CipherFunction) () int {
switch {
case Cipher3DES:
return des.BlockSize
case CipherCAST5:
return 8
case CipherAES128, CipherAES192, CipherAES256:
return 16
}
return 0
}
func ( CipherFunction) ( []byte) ( cipher.Block) {
switch {
case Cipher3DES:
, _ = des.NewTripleDESCipher()
case CipherCAST5:
, _ = cast5.NewCipher()
case CipherAES128, CipherAES192, CipherAES256:
, _ = aes.NewCipher()
}
return
}
return
}
type CompressionAlgo uint8
const (
CompressionNone CompressionAlgo = 0
CompressionZIP CompressionAlgo = 1
CompressionZLIB CompressionAlgo = 2
![]() |
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. |