NewFromKeyring reads a keyring file and creates a Signatory. If id is not the empty string, this will also try to find an Entity in the keyring whose name matches, and set that as the signing entity. It will return an error if the id is not empty and also not found.
(keyringfile, id string)
| 108 | // keyring whose name matches, and set that as the signing entity. It will return |
| 109 | // an error if the id is not empty and also not found. |
| 110 | func NewFromKeyring(keyringfile, id string) (*Signatory, error) { |
| 111 | ring, err := loadKeyRing(keyringfile) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | s := &Signatory{KeyRing: ring} |
| 117 | |
| 118 | // If the ID is empty, we can return now. |
| 119 | if id == "" { |
| 120 | return s, nil |
| 121 | } |
| 122 | |
| 123 | // We're gonna go all GnuPG on this and look for a string that _contains_. If |
| 124 | // two or more keys contain the string and none are a direct match, we error |
| 125 | // out. |
| 126 | var candidate *openpgp.Entity |
| 127 | vague := false |
| 128 | for _, e := range ring { |
| 129 | for n := range e.Identities { |
| 130 | if n == id { |
| 131 | s.Entity = e |
| 132 | return s, nil |
| 133 | } |
| 134 | if strings.Contains(n, id) { |
| 135 | if candidate != nil { |
| 136 | vague = true |
| 137 | } |
| 138 | candidate = e |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | if vague { |
| 143 | return s, fmt.Errorf("more than one key contain the id %q", id) |
| 144 | } |
| 145 | |
| 146 | s.Entity = candidate |
| 147 | return s, nil |
| 148 | } |
| 149 | |
| 150 | // PassphraseFetcher returns a passphrase for decrypting keys. |
| 151 | // |
searching dependent graphs…