Source File
armor.go
Belonging Package
golang.org/x/crypto/openpgp/armor
package armor // import "golang.org/x/crypto/openpgp/armor"
import (
)
type Block struct {
Type string // The type, taken from the preamble (i.e. "PGP SIGNATURE").
Header map[string]string // Optional headers.
Body io.Reader // A Reader from which the contents can be read
lReader lineReader
oReader openpgpReader
}
var ArmorCorrupt error = errors.StructuralError("armor invalid")
const crc24Init = 0xb704ce
const crc24Poly = 0x1864cfb
const crc24Mask = 0xffffff
type lineReader struct {
in *bufio.Reader
buf []byte
eof bool
crc uint32
crcSet bool
}
func ( *lineReader) ( []byte) ( int, error) {
if .eof {
return 0, io.EOF
}
if len(.buf) > 0 {
= copy(, .buf)
.buf = .buf[:]
return
}
, , := .in.ReadLine()
if != nil {
return
}
if {
return 0, ArmorCorrupt
}
if bytes.HasPrefix(, armorEnd) {
.eof = true
return 0, io.EOF
}
var [3]byte
var int
, = base64.StdEncoding.Decode([0:], [1:])
if != 3 || != nil {
return
}
.crc = uint32([0])<<16 |
uint32([1])<<8 |
uint32([2])
, _, = .in.ReadLine()
if != nil && != io.EOF {
return
}
if !bytes.HasPrefix(, armorEnd) {
return 0, ArmorCorrupt
}
.eof = true
.crcSet = true
return 0, io.EOF
}
if len() > 96 {
return 0, ArmorCorrupt
}
= copy(, )
:= len() -
if > 0 {
if cap(.buf) < {
.buf = make([]byte, 0, )
}
.buf = .buf[0:]
copy(.buf, [:])
}
return
}
type openpgpReader struct {
lReader *lineReader
b64Reader io.Reader
currentCRC uint32
}
func ( *openpgpReader) ( []byte) ( int, error) {
, = .b64Reader.Read()
.currentCRC = crc24(.currentCRC, [:])
if == io.EOF && .lReader.crcSet && .lReader.crc != uint32(.currentCRC&crc24Mask) {
return 0, ArmorCorrupt
}
return
}
for {
:=
, , = .ReadLine()
if != nil {
return
}
if || {
continue
}
= bytes.TrimSpace()
if len() > len(armorStart)+len(armorEndOfLine) && bytes.HasPrefix(, armorStart) {
break
}
}
= new(Block)
.Type = string([len(armorStart) : len()-len(armorEndOfLine)])
.Header = make(map[string]string)
:= false
var string
for {
:=
, , = .ReadLine()
if != nil {
= nil
return
}
if {
.Header[] += string()
continue
}
= bytes.TrimSpace()
if len() == 0 {
break
}
:= bytes.Index(, []byte(": "))
if == -1 {
goto
}
= string([:])
.Header[] = string([+2:])
}
.lReader.in =
.oReader.currentCRC = crc24Init
.oReader.lReader = &.lReader
.oReader.b64Reader = base64.NewDecoder(base64.StdEncoding, &.lReader)
.Body = &.oReader
return
![]() |
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. |