本文整理汇总了Golang中golang.org/x/crypto/openpgp.ReadArmoredKeyRing函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadArmoredKeyRing函数的具体用法?Golang ReadArmoredKeyRing怎么用?Golang ReadArmoredKeyRing使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadArmoredKeyRing函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ReadJSKey
func ReadJSKey() {
pubringFile, _ := os.Open("path to public keyring")
defer pubringFile.Close()
pubring, _ := openpgp.ReadArmoredKeyRing(pubringFile)
theirPublicKey := getKeyByEmail(pubring, "[email protected]")
secringFile, _ := os.Open("path to private keyring")
defer secringFile.Close()
secring, _ := openpgp.ReadArmoredKeyRing(secringFile)
myPrivateKey := getKeyByEmail(secring, "[email protected]")
myPrivateKey.PrivateKey.Decrypt([]byte("passphrase"))
var hint openpgp.FileHints
hint.IsBinary = false
hint.FileName = "_CONSOLE"
hint.ModTime = time.Now()
w, _ := armor.Encode(os.Stdout, "PGP MESSAGE", nil)
defer w.Close()
plaintext, _ := openpgp.Encrypt(w, []*openpgp.Entity{theirPublicKey}, myPrivateKey, &hint, nil)
defer plaintext.Close()
fmt.Fprintf(plaintext, "黄勇刚在熟悉OpenPGP代码\n")
}
示例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: Encode
// Encode - this function wraps around an Encoder object to decode the passed in data.
// @param io.Reader[] src - This parameter will be used to read the unencrypted data
// @param io.Writer[] dest - This parameter will be used to write the encrypted data
func (e *Encoder) Encode(r io.Reader, w io.Writer) error {
entitylist, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(e.Key))
if err != nil {
return err
}
// Encrypt message using public key
buf := new(bytes.Buffer)
encrypter, err := openpgp.Encrypt(buf, entitylist, nil, nil, nil)
if err != nil {
return err
}
_, err = io.Copy(encrypter, r)
if err != nil {
return err
}
err = encrypter.Close()
if err != nil {
return err
}
_, err = io.Copy(w, buf)
return err
}
示例4: SignersFromKeyring
func SignersFromKeyring(keyring string) ([]byte, error) {
var (
connectionSet = make(map[string]set)
keyIDToName = make(map[string]string)
)
ring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(keyring))
if err != nil {
return nil, err
}
for _, entity := range ring {
for _, ident := range entity.Identities {
keyID := entity.PrimaryKey.KeyIdString()
name := ident.UserId.Name
keyIDToName[keyID] = ident.Name
connectionSet[name] = set{}
for _, sig := range ident.Signatures {
if keys := ring.KeysById(*sig.IssuerKeyId); len(keys) > 0 {
for _, key := range keys {
connectionSet[name].Add(key.PublicKey.KeyIdString())
}
}
}
}
}
return newSigned(connectionSet, keyIDToName).html(), nil
}
示例5: 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, e.PrimaryKey, 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
}
示例6: ClearSign
// ClearSign the assertions using the private key and return a clear-signed buffer.
// If the private key is encrypted, the passphrase must be supplied to decrypt
// the key.
func ClearSign(assertions, armoredPrivateKey, passphrase string) ([]byte, error) {
keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKey))
if err != nil {
return nil, err
}
// Decrypt the private key, if it is encrypted
privateKey := keyring[0].PrivateKey
if privateKey.Encrypted {
err = privateKey.Decrypt([]byte(passphrase))
if err != nil {
return nil, err
}
}
// Sign the assertions and save the result in the buffer
var buf bytes.Buffer
plaintext, err := clearsign.Encode(&buf, privateKey, nil)
if err != nil {
return nil, err
}
_, err = plaintext.Write([]byte(assertions))
if err != nil {
return nil, err
}
err = plaintext.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
示例7: LoadKeyring
func LoadKeyring(path string) (openpgp.EntityList, error) {
if path == "" {
return nil, util.Errorf("no keyring configured")
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
// Accept both ASCII-armored and binary encodings
keyring, err := openpgp.ReadArmoredKeyRing(f)
if err != nil && err.Error() == "openpgp: invalid argument: no armored data found" {
offset, seekErr := f.Seek(0, os.SEEK_SET)
if offset != 0 || seekErr != nil {
return nil, util.Errorf(
"couldn't seek to beginning, got %d %s",
offset,
seekErr,
)
}
keyring, err = openpgp.ReadKeyRing(f)
}
return keyring, err
}
示例8: ParseArmorFile
// Parse a single ASCII armored PGP pubkey file and add it to the repo.
func ParseArmorFile(keyPath string) {
ascii, err := ioutil.ReadFile(keyPath)
if err != nil {
panic(err)
}
asciiReader := bytes.NewReader([]byte(ascii))
// An armored key ring can contain one or more ascii key blobs.
entityList, errReadArm := openpgp.ReadArmoredKeyRing(asciiReader)
if errReadArm != nil {
log.Println("Reading Pubkey ", errReadArm.Error())
return
}
for _, pubKeyEntity := range entityList {
if pubKeyEntity.PrimaryKey != nil {
pubKey := *pubKeyEntity.PrimaryKey
fingerprint := hex.EncodeToString(pubKey.Fingerprint[:])
GetRepo().Add(newRepoEntry(
strings.ToUpper(fingerprint),
pubKey.CreationTime.String(),
"0x"+pubKey.KeyIdString(),
"email",
"",
string(ascii[:]),
))
}
}
return
}
示例9: Decrypt
func Decrypt(data string, key_file string) (string, error) {
secretKeyring, err := os.Open(key_file)
if err != nil {
return nil, err
}
defer secretKeyring.Close()
// Taken from crypt and adapted
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(data))
entityList, err := openpgp.ReadArmoredKeyRing(secretKeyring)
if err != nil {
return nil, err
}
md, err := openpgp.ReadMessage(decoder, entityList, nil, nil)
if err != nil {
return nil, err
}
gzReader, err := gzip.NewReader(md.UnverifiedBody)
if err != nil {
return nil, err
}
defer gzReader.Close()
bytes, err := ioutil.ReadAll(gzReader)
if err != nil {
return nil, err
}
return string(bytes), nil
}
示例10: Decode
// Decode - this function wraps around a Decoder object to decode the passed in data.
// @param io.Reader[] src - This parameter will be used to read the encrypted data
// @param io.Writer[] dest - This parameter will be used to write the unencrypted data
func (d *Decoder) Decode(r io.Reader, w io.Writer) error {
entitylist, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(d.Key))
if err != nil {
return err
}
entity := entitylist[0]
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
if len(d.Passphrase) == 0 {
return errors.New("Private key is encrypted but you did not provide a passphrase")
}
err := entity.PrivateKey.Decrypt(d.Passphrase)
if err != nil {
return errors.New("Failed to decrypt private key. Did you use the wrong passphrase? (" + err.Error() + ")")
}
}
for _, subkey := range entity.Subkeys {
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
err := subkey.PrivateKey.Decrypt(d.Passphrase)
if err != nil {
return errors.New("Failed to decrypt subkey. Did you use the wrong passphrase? (" + err.Error() + ")")
}
}
}
read, err := openpgp.ReadMessage(r, entitylist, nil, nil)
if err != nil {
return err
}
_, err = io.Copy(w, read.LiteralData.Body)
return err
}
示例11: 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
}
示例12: testParse
func testParse(t *testing.T, input []byte, expected, expectedPlaintext string) {
b, rest := Decode(input)
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)
}
if !bytes.Equal(b.Bytes, []byte(expected)) {
t.Errorf("bad body, got:%x want:%x", b.Bytes, expected)
}
if !bytes.Equal(b.Plaintext, []byte(expectedPlaintext)) {
t.Errorf("bad plaintext, got:%x want:%x", b.Plaintext, expectedPlaintext)
}
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)
}
}
示例13: 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
}
示例14: 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
}
示例15: loadFile
func (kr *Keyring) loadFile(path string) error {
trustedKey, err := os.Open(path)
if err != nil {
return err
}
defer trustedKey.Close()
entityList, err := openpgp.ReadArmoredKeyRing(trustedKey)
if err != nil {
return err
}
if len(entityList) < 1 {
return errors.New("missing opengpg entity")
}
fingerprint := fingerprintToFilename(entityList[0].PrimaryKey.Fingerprint)
keyFile := filepath.Base(trustedKey.Name())
if fingerprint != keyFile {
return errors.Errorf("fingerprint mismatch: %q:%q", keyFile, fingerprint)
}
prefix, err := pathToACIdentifier(path)
if err != nil {
return err
}
kr.EntityList = append(kr.EntityList, entityList[0])
kr.paths = append(kr.paths, path)
kr.prefixes = append(kr.prefixes, prefix)
return nil
}