本文整理汇总了Golang中golang.org/x/crypto/openpgp.ReadKeyRing函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadKeyRing函数的具体用法?Golang ReadKeyRing怎么用?Golang ReadKeyRing使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadKeyRing函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: encryptOpenpgp
func encryptOpenpgp(data io.Reader, recipient string, gpghome string) ([]byte, error) {
pubkeyfile, err := os.Open(fmt.Sprintf("%s%spubring.gpg", gpghome, string(os.PathSeparator)))
if err != nil {
fmt.Println("Failed to open pubring", err)
return nil, err
}
pubring, err := openpgp.ReadKeyRing(pubkeyfile)
if err != nil {
fmt.Println("Failed to open pubring", err)
return nil, err
}
pubkey := findKey(pubring, recipient)
buf := bytes.NewBuffer(nil)
w, _ := armor.Encode(buf, "PGP MESSAGE", nil)
plaintext, err := openpgp.Encrypt(w, []*openpgp.Entity{pubkey}, nil, nil, nil)
if err != nil {
return nil, err
}
//reader := bytes.NewReader(data)
_, err = io.Copy(plaintext, data)
plaintext.Close()
w.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
示例2: processPublicKeyRing
func processPublicKeyRing() (entity *openpgp.Entity, entitylist openpgp.EntityList) {
// TODO: Handle a specified recipient
// Get default public keyring location
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
jaegerPublicKeyRing := fmt.Sprintf("%v/.gnupg/jaeger_pubring.gpg", usr.HomeDir)
publicKeyRing := ""
if _, err := os.Stat(jaegerPublicKeyRing); err == nil {
publicKeyRing = jaegerPublicKeyRing
} else {
publicKeyRing = fmt.Sprintf("%v/.gnupg/pubring.gpg", usr.HomeDir)
}
debug.Printf("publicKeyRing file:", publicKeyRing)
publicKeyRingBuffer, err := os.Open(publicKeyRing)
if err != nil {
panic(err)
}
entitylist, err = openpgp.ReadKeyRing(publicKeyRingBuffer)
if err != nil {
log.Fatal(err)
}
entity = entitylist[0]
debug.Printf("Public key default keyring:", entity.Identities)
return entity, entitylist
}
示例3: loadKeyring
func loadKeyring(keyringReader io.Reader) (openpgp.EntityList, error) {
entityList, err := openpgp.ReadKeyRing(keyringReader)
if err != nil {
return entityList, err
}
return entityList, nil
}
示例4: 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
}
示例5: enc
func enc(i int, keyRingHex string, keyName string, isSigned bool, filename string, message string, passphraseS string) error {
kring, err := openpgp.ReadKeyRing(ReaderFromHex(keyRingHex))
if err != nil {
return err
}
return Encrypt(i, kring, keyName, isSigned, filename, message)
}
示例6: 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.ReadKeyRing(keyring)
if err != nil {
return nil, err
}
return encode(data, entityList)
}
示例7: checkGPGSig
func checkGPGSig(fileName string, sigFileName string) error {
// Get a Reader for the signature file
sigFile, err := os.Open(sigFileName)
if err != nil {
return err
}
defer sigFile.Close()
// Get a Reader for the signature file
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
publicKeyBin, err := hex.DecodeString(publicKeyHex)
if err != nil {
return err
}
keyring, _ := openpgp.ReadKeyRing(bytes.NewReader(publicKeyBin))
_, err = openpgp.CheckDetachedSignature(keyring, file, sigFile)
return err
}
示例8: FetchEntity
func (fe *FileEntityFetcher) FetchEntity(keyId string) (*openpgp.Entity, error) {
f, err := wkfs.Open(fe.File)
if err != nil {
return nil, fmt.Errorf("jsonsign: FetchEntity: %v", err)
}
defer f.Close()
el, err := openpgp.ReadKeyRing(f)
if err != nil {
return nil, fmt.Errorf("jsonsign: openpgp.ReadKeyRing of %q: %v", fe.File, err)
}
for _, e := range el {
pubk := &e.PrivateKey.PublicKey
if pubk.KeyIdString() != keyId {
continue
}
if e.PrivateKey.Encrypted {
if err := fe.decryptEntity(e); err == nil {
return e, nil
} else {
return nil, err
}
}
return e, nil
}
return nil, fmt.Errorf("jsonsign: entity for keyid %q not found in %q", keyId, fe.File)
}
示例9: Insert
func Insert(publicKeyringFile, keyName, fdir, name, pass string) error {
message := pass
filename := filepath.Join(fdir, fmt.Sprintf("%s.gpg", name))
_, err := os.Stat(filename)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
return fmt.Errorf("file %s exists", filename)
}
keyringFileBuffer, err := os.Open(publicKeyringFile)
if err != nil {
return err
}
defer func() {
err := keyringFileBuffer.Close()
if err != nil {
logrus.Errorf("Error closing file %s", err)
}
}()
kring, err := openpgp.ReadKeyRing(keyringFileBuffer)
if err != nil {
return err
}
err = Encrypt(0, kring, keyName, false, filename, message)
return err
}
示例10: findKey
func findKey(key string) (*openpgp.Entity, error) {
keyId, err := strconv.ParseUint(key, 16, 64)
if err != nil {
log.Printf("Unable to parse key '%s': %s\n", key, err)
return nil, &UnknownKey{key}
}
f, err := os.Open(KeyringFile)
if err != nil {
log.Printf("Failed to open keyring: %s\n", err)
return nil, err
}
defer f.Close()
el, err := openpgp.ReadKeyRing(f)
if err != nil {
log.Printf("Failed to read keyring: %s\n", err)
return nil, err
}
for _, entity := range el {
if entity.PrimaryKey.KeyId&0xFFFFFFFF == keyId {
return entity, nil
}
for _, key := range entity.Subkeys {
if key.PublicKey.KeyId&0xFFFFFFFF == keyId {
return entity, nil
}
}
}
return nil, &UnknownKey{key}
}
示例11: processSecretKeyRing
func processSecretKeyRing() (entity *openpgp.Entity, entitylist openpgp.EntityList) {
// Get default secret keyring location
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
jaegerSecretKeyRing := fmt.Sprintf("%v/.gnupg/jaeger_secring.gpg", usr.HomeDir)
secretKeyRing := ""
if _, err := os.Stat(jaegerSecretKeyRing); err == nil {
secretKeyRing = jaegerSecretKeyRing
} else {
secretKeyRing = fmt.Sprintf("%v/.gnupg/secring.gpg", usr.HomeDir)
}
debug.Printf("secretKeyRing file:", secretKeyRing)
secretKeyRingBuffer, err := os.Open(secretKeyRing)
if err != nil {
panic(err)
}
entitylist, err = openpgp.ReadKeyRing(secretKeyRingBuffer)
if err != nil {
log.Fatal(err)
}
entity = entitylist[0]
debug.Printf("Private key default keyring:", entity.Identities)
return entity, entitylist
}
示例12: main
func main() {
//NewEntity("hyg", "[email protected]", "secfile.key")
//return
pubringFile, _ := os.Open("path to public keyring")
defer pubringFile.Close()
pubring, _ := openpgp.ReadKeyRing(pubringFile)
//theirPublicKey := getKeyByEmail(pubring, "[email protected]")
theirPublicKey := getKeyByEmail(pubring, "[email protected]")
secringFile, _ := os.Open("path to private keyring")
defer secringFile.Close()
sevring, _ := openpgp.ReadKeyRing(secringFile)
myPrivateKey := getKeyByEmail(sevring, "[email protected]")
//theirPublicKey.Serialize(os.Stdout)
//myPrivateKey.Serialize(os.Stdout)
//myPrivateKey.SerializePrivate(os.Stdout, nil)
myPrivateKey.PrivateKey.Decrypt([]byte("passphrase"))
/*
// bug: have to input the correct passphrase at the first time
for myPrivateKey.PrivateKey.Encrypted {
fmt.Print("PGP passphrase: ")
pgppass := gopass.GetPasswd()
myPrivateKey.PrivateKey.Decrypt([]byte(pgppass))
if myPrivateKey.PrivateKey.Encrypted {
fmt.Println("Incorrect. Try again or press ctrl+c to exit.")
}
}
*/
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")
}
示例13: ReadKeyRing
func ReadKeyRing(keyRingName string) (openpgp.EntityList, error) {
keyfile, err := os.Open(keyRingName)
if err != nil {
return nil, err
}
defer keyfile.Close()
return openpgp.ReadKeyRing(keyfile)
}
示例14: EncodeWith
func EncodeWith(data []byte, keyring io.Reader, f EntityFilter) ([]byte, error) {
entityList, err := openpgp.ReadKeyRing(keyring)
if err != nil {
return nil, err
}
return encode(data, f.Entities(entityList...))
}
示例15: loadKeyRing
func loadKeyRing(ringpath string) (openpgp.EntityList, error) {
f, err := os.Open(ringpath)
if err != nil {
return nil, err
}
defer f.Close()
return openpgp.ReadKeyRing(f)
}