本文整理汇总了Golang中golang.org/x/crypto/openpgp.ReadArmoredKeyRing函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadArmoredKeyRing函数的具体用法?Golang ReadArmoredKeyRing怎么用?Golang ReadArmoredKeyRing使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadArmoredKeyRing函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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")
}
开发者ID:hyg,项目名称:go.sample,代码行数:25,代码来源:main.go
示例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
}
开发者ID:ContiGuy,项目名称:conti-gui,代码行数:28,代码来源:secconf.go
示例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
}
开发者ID:mjbruce,项目名称:capstone,代码行数:29,代码来源:mypgp.go
示例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
}
开发者ID:calebthompson,项目名称:pgf,代码行数:29,代码来源:signers.go
示例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
}
开发者ID:jpn592,项目名称:keybase,代码行数:34,代码来源:openpgp.go
示例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
}
开发者ID:slimjim777,项目名称:factorysign,代码行数:34,代码来源:sign.go
示例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
}
开发者ID:petertseng,项目名称:p2,代码行数:26,代码来源:auth_policy.go
示例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
}
开发者ID:cooox,项目名称:fingerprinter,代码行数:33,代码来源:pgp.go
示例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
}
开发者ID:ludiazv,项目名称:confd,代码行数:29,代码来源:template_funcs.go
示例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
}
开发者ID:mjbruce,项目名称:capstone,代码行数:37,代码来源:mypgp.go
示例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
}
开发者ID:igofman,项目名称:mig,代码行数:33,代码来源:pgp.go
示例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)
}
}
开发者ID:GamerockSA,项目名称:dex,代码行数:28,代码来源:clearsign_test.go
示例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
}
开发者ID:igofman,项目名称:mig,代码行数:34,代码来源:pgp.go
示例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
}
开发者ID:igofman,项目名称:mig,代码行数:25,代码来源:pgp.go
示例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
}
开发者ID:saper,项目名称:jetpack,代码行数:30,代码来源:keyring.go
示例16: getAccountPublicKey
func getAccountPublicKey(account *models.Account) (*openpgp.Entity, error) {
if account.PublicKey != "" {
cursor, err := gorethink.Db(cfg.RethinkDatabase).Table("keys").Get(account.PublicKey).Run(session)
if err != nil {
return nil, err
}
defer cursor.Close()
var key *models.Key
if err := cursor.One(&key); err != nil {
return nil, err
}
keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(key.Key))
if err != nil {
return nil, err
}
return keyring[0], nil
}
cursor, err := gorethink.Db(cfg.RethinkDatabase).Table("keys").GetAllByIndex("owner", account.ID).Run(session)
if err != nil {
return nil, err
}
defer cursor.Close()
var keys []*models.Key
if err := cursor.All(&keys); err != nil {
return nil, err
}
if len(keys) == 0 {
return nil, fmt.Errorf("Recipient has no public key")
}
keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(keys[0].Key))
if err != nil {
return nil, err
}
return keyring[0], nil
}
开发者ID:Zenithar,项目名称:mailer,代码行数:43,代码来源:handler.go
示例17: loadEntities
func (h *testHarness) loadEntities() []*openpgp.Entity {
if h.Err != nil {
return nil
}
keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(testUsers))
if err != nil {
h.Err = err
return nil
}
return keyring
}
开发者ID:rudle,项目名称:p2,代码行数:11,代码来源:auth_policy_test.go
示例18: reviewKey
func reviewKey(prefix types.ACIdentifier, key *os.File, fingerprint string) (bool, error) {
defer key.Seek(0, os.SEEK_SET)
kr, err := openpgp.ReadArmoredKeyRing(key)
if err != nil {
return false, errors.Trace(err)
}
if prefix == Root {
fmt.Println("Prefix: ROOT KEY (matches all names)")
} else {
fmt.Println("Prefix:", prefix)
}
for _, k := range kr {
fmt.Println(KeyDescription(k))
}
if fingerprint == "" {
// TODO: use UI, check if interactive
in := bufio.NewReader(os.Stdin)
for {
fmt.Printf("Are you sure you want to trust this key (yes/no)? ")
input, err := in.ReadString('\n')
if err != nil {
return false, errors.Errorf("error reading input: %v", err)
}
switch input {
case "yes\n":
return true, nil
case "no\n":
return false, nil
default:
fmt.Printf("Please enter 'yes' or 'no'")
}
}
} else if fpBytes, err := hex.DecodeString(
strings.Map(
// Strip spaces
func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, fingerprint)); err != nil {
return false, errors.Trace(err)
} else if bytes.Compare(fpBytes, kr[0].PrimaryKey.Fingerprint[:]) != 0 {
fmt.Printf("Fingerprint mismatch (expected %#v)\n", fingerprint)
return false, nil
} else {
return true, nil
}
return true, nil
}
开发者ID:saper,项目名称:jetpack,代码行数:53,代码来源:util.go
示例19: NewDetachedSignature
// NewDetachedSignature creates a new openpgp armored detached signature for the given ACI
// signed with armoredPrivateKey.
func NewDetachedSignature(armoredPrivateKey string, aci io.Reader) (io.Reader, error) {
entityList, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKey))
if err != nil {
return nil, err
}
if len(entityList) < 1 {
return nil, errors.New("empty entity list")
}
signature := &bytes.Buffer{}
if err := openpgp.ArmoredDetachSign(signature, entityList[0], aci, nil); err != nil {
return nil, err
}
return signature, nil
}
开发者ID:nhlfr,项目名称:rkt,代码行数:16,代码来源:aci.go
示例20: ApprovalCheck
func ApprovalCheck(PolicyFile, SignaturesFile, Id string) (bool, error) {
var (
commit SignedCommit // Commit corresponding to be verified
developers openpgp.EntityList // List of all developers whose public keys are in the policy file
approvers map[string]*openpgp.Entity // Map of developers who provided a valid signature. Indexed by public key id (openpgp.PrimaryKey.KeyIdString)
err error
)
commit.Policy.Threshold, commit.Policy.DevPubKeys, commit.Policy.CothKey, err = PolicyScanner(PolicyFile)
checkFileError(err, PolicyFile)
commit.Signatures, err = SigScanner(SignaturesFile)
checkFileError(err, SignaturesFile)
commit.CommitID = Id
// commit.CommitID, err = CommitScanner(CommitIdFile)
// checkFileError(err, CommitIdFile)
// commit.Approval = false
approvers = make(map[string]*openpgp.Entity)
// Creating openpgp entitylist from list of public keys
developers = make(openpgp.EntityList, 0)
for _, pubkey := range commit.Policy.DevPubKeys {
keybuf, err := openpgp.ReadArmoredKeyRing(strings.NewReader(pubkey))
if err != nil {
dbg.Error("Could not decode armored public key", err)
}
for _, entity := range keybuf {
developers = append(developers, entity)
}
}
// Verifying every signature in the list and counting valid ones
for _, signature := range commit.Signatures {
result, err := openpgp.CheckArmoredDetachedSignature(developers, bytes.NewBufferString(commit.CommitID), strings.NewReader(signature))
if err != nil {
dbg.Lvl1("The signature is invalid or cannot be verified due to", err)
} else {
if approvers[result.PrimaryKey.KeyIdString()] == nil { // We need to check that this is a unique signature
approvers[result.PrimaryKey.KeyIdString()] = result
dbg.Lvl3("Approver: %+v", result.Identities)
}
}
}
dbg.Lvl3("Is release approved? ", len(approvers) >= commit.Policy.Threshold)
// commit.Approval = (len(approvers) >= commit.Policy.Threshold)
return len(approvers) >= commit.Policy.Threshold, err
// return commit, err
}
开发者ID:nikirill,项目名称:cothority,代码行数:50,代码来源:verification.go
注:本文中的golang.org/x/crypto/openpgp.ReadArmoredKeyRing函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论