本文整理汇总了Golang中golang.org/x/crypto/openpgp/packet.NewReader函数的典型用法代码示例。如果您正苦于以下问题:Golang NewReader函数的具体用法?Golang NewReader怎么用?Golang NewReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewReader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DecryptBytes
// DecryptBytes takes in base64-encoded encrypted bytes and the base64-encoded
// private key and decrypts it. A bytes.Buffer is returned to allow the caller
// to do useful thing with it (get it as a []byte, get it as a string, use it
// as an io.Reader, etc), and also because this function doesn't know if what
// comes out is binary data or a string, so let the caller decide.
func DecryptBytes(encodedCrypt, privKey string) (*bytes.Buffer, error) {
privKeyBytes, err := base64.StdEncoding.DecodeString(privKey)
if err != nil {
return nil, fmt.Errorf("Error decoding base64 private key: %s", err)
}
cryptBytes, err := base64.StdEncoding.DecodeString(encodedCrypt)
if err != nil {
return nil, fmt.Errorf("Error decoding base64 crypted bytes: %s", err)
}
entity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(privKeyBytes)))
if err != nil {
return nil, fmt.Errorf("Error parsing private key: %s", err)
}
entityList := &openpgp.EntityList{entity}
md, err := openpgp.ReadMessage(bytes.NewBuffer(cryptBytes), entityList, nil, nil)
if err != nil {
return nil, fmt.Errorf("Error decrypting the messages: %s", err)
}
ptBuf := bytes.NewBuffer(nil)
ptBuf.ReadFrom(md.UnverifiedBody)
return ptBuf, nil
}
示例2: TestFetchKeybasePubkeys
func TestFetchKeybasePubkeys(t *testing.T) {
testset := []string{"keybase:jefferai", "keybase:hashicorp"}
ret, err := FetchKeybasePubkeys(testset)
if err != nil {
t.Fatalf("bad: %v", err)
}
fingerprints := []string{}
for _, user := range testset {
data, err := base64.StdEncoding.DecodeString(ret[user])
if err != nil {
t.Fatalf("error decoding key for user %s: %v", user, err)
}
entity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(data)))
if err != nil {
t.Fatalf("error parsing key for user %s: %v", user, err)
}
fingerprints = append(fingerprints, hex.EncodeToString(entity.PrimaryKey.Fingerprint[:]))
}
exp := []string{
"0f801f518ec853daff611e836528efcac6caa3db",
"91a6e7f85d05c65630bef18951852d87348ffc4c",
}
if !reflect.DeepEqual(fingerprints, exp) {
t.Fatalf("fingerprints do not match; expected \n%#v\ngot\n%#v\n", exp, fingerprints)
}
}
示例3: EncryptShares
// EncryptShares takes an ordered set of Shamir key share fragments and
// PGP public keys and encrypts each Shamir key fragment with the corresponding
// public key
//
// Note: There is no corresponding test function; this functionality is
// thoroughly tested in the init and rekey command unit tests
func EncryptShares(secretShares [][]byte, pgpKeys []string) ([][]byte, error) {
if len(secretShares) != len(pgpKeys) {
return nil, fmt.Errorf("Mismatch between number of generated shares and number of PGP keys")
}
encryptedShares := [][]byte{}
for i, keystring := range pgpKeys {
data, err := base64.StdEncoding.DecodeString(keystring)
if err != nil {
return nil, fmt.Errorf("Error decoding given PGP key: %s", err)
}
entity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(data)))
if err != nil {
return nil, fmt.Errorf("Error parsing given PGP key: %s", err)
}
ctBuf := bytes.NewBuffer(nil)
pt, err := openpgp.Encrypt(ctBuf, []*openpgp.Entity{entity}, nil, nil, nil)
if err != nil {
return nil, fmt.Errorf("Error setting up encryption for PGP message: %s", err)
}
_, err = pt.Write([]byte(hex.EncodeToString(secretShares[i])))
if err != nil {
return nil, fmt.Errorf("Error encrypting PGP message: %s", err)
}
pt.Close()
encryptedShares = append(encryptedShares, ctBuf.Bytes())
}
return encryptedShares, nil
}
示例4: ReadKeyRing
// ReadKeyRing reads one or more public/private keys. Unsupported keys are
// ignored as long as at least a single valid key is found.
func ReadKeyRing(r io.Reader) (el EntityList, err error) {
packets := packet.NewReader(r)
var lastUnsupportedError error
for {
var e *Entity
e, err = ReadEntity(packets)
if err != nil {
// TODO: warn about skipped unsupported/unreadable keys
if _, ok := err.(errors.UnsupportedError); ok {
lastUnsupportedError = err
err = readToNextPublicKey(packets)
} else if _, ok := err.(errors.StructuralError); ok {
// Skip unreadable, badly-formatted keys
lastUnsupportedError = err
err = readToNextPublicKey(packets)
}
if err == io.EOF {
err = nil
break
}
if err != nil {
el = nil
break
}
} else {
el = append(el, e)
}
}
if len(el) == 0 && err == nil {
err = lastUnsupportedError
}
return
}
示例5: verifyDetachedSignature
func verifyDetachedSignature(key *packet.PublicKey, contentf, sigf io.Reader) error {
var hashFunc crypto.Hash
packets := packet.NewReader(sigf)
p, err := packets.Next()
if err != nil {
return err
}
switch sig := p.(type) {
case *packet.Signature:
hashFunc = sig.Hash
case *packet.SignatureV3:
hashFunc = sig.Hash
default:
return errors.UnsupportedError("unrecognized signature")
}
h := hashFunc.New()
if _, err := io.Copy(h, contentf); err != nil && err != io.EOF {
return err
}
switch sig := p.(type) {
case *packet.Signature:
err = key.VerifySignature(h, sig)
case *packet.SignatureV3:
err = key.VerifySignatureV3(h, sig)
default:
panic("unreachable")
}
return err
}
示例6: testSignedManifest
func testSignedManifest(t *testing.T, modify func(*pods.Manifest, *openpgp.Entity)) (*pods.Manifest, *openpgp.Entity) {
testManifest := testManifest(t)
if fakeSigner == nil {
var err error
fakeSigner, err = openpgp.ReadEntity(packet.NewReader(bytes.NewReader(fakeEntity)))
Assert(t).IsNil(err, "should have read entity")
}
if modify != nil {
modify(testManifest, fakeSigner)
}
manifestBytes, err := testManifest.Marshal()
Assert(t).IsNil(err, "manifest bytes error should have been nil")
var buf bytes.Buffer
sigWriter, err := clearsign.Encode(&buf, fakeSigner.PrivateKey, nil)
Assert(t).IsNil(err, "clearsign Encode error should have been nil")
sigWriter.Write(manifestBytes)
sigWriter.Close()
manifest, err := pods.ManifestFromBytes(buf.Bytes())
Assert(t).IsNil(err, "should have generated manifest from signed bytes")
return manifest, fakeSigner
}
示例7: loadKey
// loadKey loads a GPG key found at a particular path.
func loadKey(keypath string) (*openpgp.Entity, error) {
f, err := os.Open(keypath)
if err != nil {
return nil, err
}
defer f.Close()
pr := packet.NewReader(f)
return openpgp.ReadEntity(pr)
}
示例8: PublicKeyToPGPClient
func PublicKeyToPGPClient(publicKey string) PGPClient {
block, err := armor.Decode(strings.NewReader(publicKey))
if err != nil {
panic(err)
}
entity, err := openpgp.ReadEntity(packet.NewReader(block.Body))
if err != nil {
panic(err)
}
return PGPClient{openpgp.EntityList{entity}}
}
示例9: readPublicSigningKey
func readPublicSigningKey(keyf io.Reader) (*packet.PublicKey, error) {
keypackets := packet.NewReader(keyf)
p, err := keypackets.Next()
if err != nil {
return nil, err
}
switch pkt := p.(type) {
case *packet.PublicKey:
debug("key: ", pkt)
return pkt, nil
default:
log.Printf("ReadPublicSigningKey: got %T, want *packet.PublicKey", pkt)
}
return nil, errors.StructuralError("expected first packet to be PublicKey")
}
示例10: GetEntities
// GetEntities takes in a string array of base64-encoded PGP keys and returns
// the openpgp Entities
func GetEntities(pgpKeys []string) ([]*openpgp.Entity, error) {
ret := make([]*openpgp.Entity, 0, len(pgpKeys))
for _, keystring := range pgpKeys {
data, err := base64.StdEncoding.DecodeString(keystring)
if err != nil {
return nil, fmt.Errorf("Error decoding given PGP key: %s", err)
}
entity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(data)))
if err != nil {
return nil, fmt.Errorf("Error parsing given PGP key: %s", err)
}
ret = append(ret, entity)
}
return ret, nil
}
示例11: encryptEmail
func encryptEmail(e *Email) error {
if len(e.OpenPGPEncryptTo) == 0 {
return nil
}
buf := &bytes.Buffer{}
var destEntities []*openpgp.Entity
for _, eto := range e.OpenPGPEncryptTo {
r := bytes.NewBufferString(eto)
blk, err := armor.Decode(r)
if err != nil {
return err
}
rr := packet.NewReader(blk.Body)
e, err := openpgp.ReadEntity(rr)
if err != nil {
return err
}
destEntities = append(destEntities, e)
}
aew, err := armor.Encode(buf, "PGP MESSAGE", map[string]string{
"Version": "OpenPGP",
})
if err != nil {
return err
}
wr, err := openpgp.Encrypt(aew, destEntities, nil, nil, nil)
if err != nil {
return err
}
_, err = wr.Write([]byte(e.Body))
if err != nil {
wr.Close()
return err
}
wr.Close()
aew.Close()
e.Body = string(buf.Bytes())
return nil
}
示例12: TestPubKeyFilesFlagSetKeybase
func TestPubKeyFilesFlagSetKeybase(t *testing.T) {
tempDir, err := ioutil.TempDir("", "vault-test")
if err != nil {
t.Fatalf("Error creating temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)
err = ioutil.WriteFile(tempDir+"/pubkey2", []byte(pubKey2), 0755)
if err != nil {
t.Fatalf("Error writing pub key 2 to temp file: %s", err)
}
pkf := new(PubKeyFilesFlag)
err = pkf.Set("keybase:jefferai,@" + tempDir + "/pubkey2" + ",keybase:hashicorp")
if err != nil {
t.Fatalf("err: %s", err)
}
fingerprints := []string{}
for _, pubkey := range []string(*pkf) {
keyBytes, err := base64.StdEncoding.DecodeString(pubkey)
if err != nil {
t.Fatalf("bad: %v", err)
}
pubKeyBuf := bytes.NewBuffer(keyBytes)
reader := packet.NewReader(pubKeyBuf)
entity, err := openpgp.ReadEntity(reader)
if err != nil {
t.Fatalf("bad: %v", err)
}
if entity == nil {
t.Fatalf("nil entity encountered")
}
fingerprints = append(fingerprints, hex.EncodeToString(entity.PrimaryKey.Fingerprint[:]))
}
exp := []string{
"0f801f518ec853daff611e836528efcac6caa3db",
"cf3d4694c9f57b28cb4092c2eb832c67eb5e8957",
"91a6e7f85d05c65630bef18951852d87348ffc4c",
}
if !reflect.DeepEqual(fingerprints, exp) {
t.Fatalf("bad: got \n%#v\nexpected\n%#v\n", fingerprints, exp)
}
}
示例13: newExtPGPPrivateKey
func newExtPGPPrivateKey(exportedPubKeyStream io.Reader, from string, sign func(content []byte) ([]byte, error)) (*extPGPPrivateKey, error) {
var pubKey *packet.PublicKey
rd := packet.NewReader(exportedPubKeyStream)
for {
pkt, err := rd.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("cannot read exported public key: %v", err)
}
cand, ok := pkt.(*packet.PublicKey)
if ok {
if cand.IsSubkey {
continue
}
if pubKey != nil {
return nil, fmt.Errorf("cannot select exported public key, found many")
}
pubKey = cand
}
}
if pubKey == nil {
return nil, fmt.Errorf("cannot read exported public key, found none (broken export)")
}
rsaPubKey, ok := pubKey.PublicKey.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("not a RSA key")
}
return &extPGPPrivateKey{
pubKey: RSAPublicKey(rsaPubKey),
from: from,
pgpFingerprint: fmt.Sprintf("%X", pubKey.Fingerprint),
bitLen: rsaPubKey.N.BitLen(),
doSign: sign,
}, nil
}
示例14: SetKey
func (o *openPGP) SetKey(k key) (err error) {
keyPath := filepath.Join(conf.mountPoint, k.Path)
keyFile, err := os.Open(keyPath)
if err != nil {
return
}
defer keyFile.Close()
keyBlock, err := armor.Decode(keyFile)
if err != nil {
return
}
reader := packet.NewReader(keyBlock.Body)
entity, err := openpgp.ReadEntity(reader)
if err != nil {
return
}
switch keyBlock.Type {
case openpgp.PrivateKeyType:
if k.Private != true {
return fmt.Errorf("public key detected in private key slot")
}
o.secKey = entity
case openpgp.PublicKeyType:
if k.Private == true {
return fmt.Errorf("private key detected in public key slot")
}
o.pubKey = entity
default:
return fmt.Errorf("key type error: %s", keyBlock.Type)
}
return
}
示例15: Import
// Import private key from defined path.
func Import(privKeyPath string) (*PGP, error) {
privKeyFile, err := os.Open(privKeyPath)
if err != nil {
return nil, err
}
defer privKeyFile.Close()
file := packet.NewReader(bufio.NewReader(privKeyFile))
entity, err := openpgp.ReadEntity(file)
if err != nil {
return nil, errwrap.Wrapf(err, "can't read entity from path")
}
obj := &PGP{
Entity: entity,
}
log.Printf("PGP: Imported key: %s", obj.Entity.PrivateKey.KeyIdShortString())
return obj, nil
}