本文整理匯總了Golang中code/google/com/p/go/crypto/openpgp.ReadArmoredKeyRing函數的典型用法代碼示例。如果您正苦於以下問題:Golang ReadArmoredKeyRing函數的具體用法?Golang ReadArmoredKeyRing怎麽用?Golang ReadArmoredKeyRing使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ReadArmoredKeyRing函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Import
// Import imports an armoured public key block.
func (keyRing *KeyRing) Import(armoured string) (n int, err error) {
buf := bytes.NewBufferString(armoured)
el, err := openpgp.ReadArmoredKeyRing(buf)
if err != nil {
return
}
for _, e := range el {
if keyRing.private && e.PrivateKey == nil {
err = ErrSecRing
return
} else if !keyRing.private && e.PrivateKey != nil {
err = ErrPubRing
return
}
for name, id := range e.Identities {
err = e.PrimaryKey.VerifyUserIdSignature(name, id.SelfSignature)
if err != nil {
return
}
}
}
for _, e := range el {
id := fmt.Sprintf("%x", e.PrimaryKey.Fingerprint)
if _, ok := keyRing.Entities[id]; !ok {
keyRing.Entities[id] = e
n++
}
}
return
}
示例2: Encode
// Encode encodes data to a base64 encoded using the secconf codec.
// data is encrypted with all public keys found in the supplied keyring.
func Encode(data []byte, keyring io.Reader) ([]byte, error) {
entityList, err := openpgp.ReadArmoredKeyRing(keyring)
if err != nil {
return nil, err
}
buffer := new(bytes.Buffer)
encoder := base64.NewEncoder(base64.StdEncoding, buffer)
pgpWriter, err := openpgp.Encrypt(encoder, entityList, nil, nil, nil)
if err != nil {
return nil, err
}
gzWriter := gzip.NewWriter(pgpWriter)
if _, err := gzWriter.Write(data); err != nil {
return nil, err
}
if err := gzWriter.Close(); err != nil {
return nil, err
}
if err := pgpWriter.Close(); err != nil {
return nil, err
}
if err := encoder.Close(); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
示例3: TestParse
func TestParse(t *testing.T) {
b, rest := Decode(clearsignInput)
if b == nil {
t.Fatal("failed to decode clearsign message")
}
if !bytes.Equal(rest, []byte("trailing")) {
t.Errorf("unexpected remaining bytes returned: %s", string(rest))
}
if b.ArmoredSignature.Type != "PGP SIGNATURE" {
t.Errorf("bad armor type, got:%s, want:PGP SIGNATURE", b.ArmoredSignature.Type)
}
expected := []byte("Hello world\r\nline 2")
if !bytes.Equal(b.Bytes, expected) {
t.Errorf("bad body, got:%x want:%x", b.Bytes, expected)
}
expected = []byte("Hello world\nline 2\n")
if !bytes.Equal(b.Plaintext, expected) {
t.Errorf("bad plaintext, got:%x want:%x", b.Plaintext, expected)
}
keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(signingKey))
if err != nil {
t.Errorf("failed to parse public key: %s", err)
}
if _, err := openpgp.CheckDetachedSignature(keyring, bytes.NewBuffer(b.Bytes), b.ArmoredSignature.Body); err != nil {
t.Errorf("failed to check signature: %s", err)
}
}
示例4: ArmoredKeysToKeyring
// ArmoredKeysToKeyring takes a list of PGP keys in armored form and transforms
// it into a keyring that can be used in other openpgp's functions
func ArmoredKeysToKeyring(keys [][]byte) (keyring io.ReadSeeker, keycount int, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("ArmoredPubKeysToKeyRing() -> %v", e)
}
}()
var buf bytes.Buffer
// iterate over the keys, and load them into an io.Reader keyring
for i, key := range keys {
// Load PGP public key
el, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(key))
if err != nil {
panic(fmt.Errorf("key num.%d failed to load with error %v", i, err))
}
keycount += 1
if len(el) != 1 {
err = fmt.Errorf("PGP key contains %d entities, wanted 1", len(el))
panic(err)
}
// serialize entities into io.Reader
if el[0].PrivateKey != nil {
err = el[0].SerializePrivate(&buf, nil)
} else {
err = el[0].Serialize(&buf)
}
if err != nil {
panic(err)
}
}
keyring = bytes.NewReader(buf.Bytes())
return
}
示例5: KeyringToArmoredPubKeys
// KeyringToArmoredPubKeys reads all public keys from a keyring and returned their armored format
// into map of keys indexed by key fingerprint
func KeyringToArmoredPubKeys(keyring io.ReadCloser) (armoredkeys map[string][]byte, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("KeyringToArmoredPubKeys() -> %v", e)
}
}()
els, err := openpgp.ReadArmoredKeyRing(keyring)
if err != nil {
panic(err)
}
for _, el := range els {
fingerprint := hex.EncodeToString(el.PrimaryKey.Fingerprint[:])
var pubkeybuf bytes.Buffer
err = el.Serialize(&pubkeybuf)
if err != nil {
panic(err)
}
armoredbuf := bytes.NewBuffer(nil)
ewrbuf, err := armor.Encode(armoredbuf, openpgp.PublicKeyType, nil)
if err != nil {
panic(err)
}
_, err = ewrbuf.Write(pubkeybuf.Bytes())
if err != nil {
panic(err)
}
ewrbuf.Close()
armoredkeys[fingerprint] = armoredbuf.Bytes()
}
return
}
示例6: ArmorPubKey
func ArmorPubKey(pubkey []byte) (armoredPubKey []byte, err error) {
var pubkeybuf bytes.Buffer
// Load PGP public key
el, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(pubkey))
if err != nil {
panic(err)
}
// serialize entities into io.Reader
err = el[0].Serialize(&pubkeybuf)
if err != nil {
panic(err)
}
armoredbuf := bytes.NewBuffer(nil)
ewrbuf, err := armor.Encode(armoredbuf, openpgp.PublicKeyType, nil)
if err != nil {
panic(err)
}
_, err = ewrbuf.Write(pubkeybuf.Bytes())
if err != nil {
panic(err)
}
ewrbuf.Close()
armoredPubKey = armoredbuf.Bytes()
return
}
示例7: GetPri
//GetPri gets packet.PrivateKEy and openpgp.Entity of Decrypted Private Key from ascii armor
func GetPri(asciiPri string, pripwd string) (priKey packet.PrivateKey, priEnt openpgp.Entity, err error) {
read1 := bytes.NewReader([]byte(asciiPri))
entityList, errReadArm := openpgp.ReadArmoredKeyRing(read1)
if errReadArm != nil {
// fmt.Println("Reading PriKey ", errReadArm.Error())
err = errReadArm
return
}
for _, can_pri := range entityList {
smPr := can_pri.PrivateKey
retEntity := can_pri
if smPr == nil {
// fmt.Println("No Private Key")
err = errors.New("No private key found in armor")
return
}
priKey = *smPr
errDecr := priKey.Decrypt([]byte(pripwd))
if errDecr != nil {
// fmt.Println("Decrypting ", errDecr.Error())
err = errDecr
return
}
retEntity.PrivateKey = &priKey
priEnt = *retEntity
}
return
}
示例8: ArmoredPubKeysToKeyring
// TransformArmoredPubKeysToKeyring takes a list of public PGP key in armored form and transforms
// it into a keyring that can be used in other openpgp's functions
func ArmoredPubKeysToKeyring(pubkeys []string) (keyring io.Reader, keycount int, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("ArmoredePubKeysToKeyRing() -> %v", e)
}
}()
var buf bytes.Buffer
// iterate over the keys, and load them into an io.Reader keyring
for _, key := range pubkeys {
// Load PGP public key
el, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(key))
if err != nil {
panic(err)
}
keycount += 1
if len(el) != 1 {
err = fmt.Errorf("Public GPG Key contains %d entities, wanted 1\n", len(el))
panic(err)
}
// serialize entities into io.Reader
err = el[0].Serialize(&buf)
if err != nil {
panic(err)
}
}
keyring = bytes.NewReader(buf.Bytes())
return
}
示例9: newPGPFilter
func newPGPFilter(conf map[string]string) (filter, error) {
if conf["pubkey"] == "" {
return nil, errors.New("pubkey required")
}
to, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer([]byte(conf["pubkey"])))
if err != nil {
return nil, fmt.Errorf("Couldn't read pubkey: %s", err)
}
pr, pw := io.Pipe()
f := &pgpFilter{
r: pr,
pw: pw,
ready: make(chan bool),
}
go func() {
w, err := openpgp.Encrypt(pw, to, nil, nil, nil) // Not signing yet, sorry
f.pgpw = w
f.ready <- true
if err != nil {
pr.CloseWithError(err)
pw.CloseWithError(err)
return
}
log.Print("enc returned")
}()
return f, nil
}
示例10: toEntity
func toEntity(k string) *openpgp.Entity {
es, err := openpgp.ReadArmoredKeyRing(strings.NewReader(k))
if err != nil {
logger.Panicf("Error reading key: %v", err)
} else if len(es) != 1 {
logger.Panicf("Expecting a single entity, got len = %d", len(es))
}
return es[0]
}
示例11: toEntity
func toEntity(k string) *openpgp.Entity {
es, err := openpgp.ReadArmoredKeyRing(strings.NewReader(k))
if err != nil {
log.Fatal("Error reading key", err)
} else if len(es) != 1 {
log.Fatal("Expecting a single entity, len = ", len(es))
}
return es[0]
}
示例12: newUnpgpFilter
func newUnpgpFilter(conf map[string]string) (filter, error) {
if conf["privatkey"] == "" {
return nil, errors.New("privatkey required")
}
keyRing, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer([]byte(conf["privatkey"])))
if err != nil {
return nil, fmt.Errorf("Couldn't read privatkey: %s", err)
}
return &unpgpFilter{
keyRing: keyRing,
ready: make(chan bool),
}, nil
}
示例13: processArmoredKeyRingFile
func processArmoredKeyRingFile(keyringFile *string) (entity *openpgp.Entity, entitylist openpgp.EntityList) {
keyringFileBuffer, err := os.Open(*keyringFile)
if err != nil {
log.Fatalln("ERROR: Unable to read keyring file")
}
entitylist, err = openpgp.ReadArmoredKeyRing(keyringFileBuffer)
if err != nil {
log.Fatal(err)
}
entity = entitylist[0]
debug.Printf("Public key from ASCII armored string:", entity.Identities)
return entity, entitylist
}
示例14: VerifySignature
func VerifySignature(pubKey, signed, signatureArmor string) bool {
keyRing, err := openpgp.ReadArmoredKeyRing(strings.NewReader(pubKey))
if err != nil {
panic(err)
}
signer, err := openpgp.CheckArmoredDetachedSignature(
keyRing,
strings.NewReader(signed),
strings.NewReader(signatureArmor),
)
if err != nil {
panic(err)
}
return signer != nil
}
示例15: GetPub
//GetPub gets packet.PublicKey and openpgp.Entity of Public Key from ascii armor
func GetPub(asciiPub string) (pubKey packet.PublicKey, retEntity openpgp.Entity, err error) {
read1 := bytes.NewReader([]byte(asciiPub))
entityList, errReadArm := openpgp.ReadArmoredKeyRing(read1)
if errReadArm != nil {
err = errReadArm
return
}
for _, pubKeyEntity := range entityList {
if pubKeyEntity.PrimaryKey != nil {
pubKey = *pubKeyEntity.PrimaryKey
retEntity = *pubKeyEntity
}
}
return
}