本文整理汇总了Golang中code/google/com/p/go/crypto/openpgp/armor.Encode函数的典型用法代码示例。如果您正苦于以下问题:Golang Encode函数的具体用法?Golang Encode怎么用?Golang Encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SerializeKeys
func SerializeKeys(entity *openpgp.Entity) (privKeyArmor, pubKeyArmor string, err error) {
// First serialize the private parts.
// NOTE: need to call this in order to initialize the newly created entities,
// otherwise entity.Serialize() will fail
// https://code.google.com/p/go/issues/detail?id=6483
b := bytes.NewBuffer(nil)
w, _ := armor.Encode(b, openpgp.PrivateKeyType, nil)
err = entity.SerializePrivate(w, nil)
if err != nil {
return "", "", err
}
w.Close()
privKeyArmor = b.String()
// Serialize the public key.
b.Reset()
w, _ = armor.Encode(b, openpgp.PublicKeyType, nil)
err = entity.Serialize(w)
if err != nil {
return "", "", err
}
w.Close()
pubKeyArmor = b.String()
return
}
示例2: GenerateKeyPair
func GenerateKeyPair(name, desc, email string) (pubkey, privkey []byte, fp string, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("GenerateKeyPair() -> %v", e)
}
}()
// generate a private key
ent, err := openpgp.NewEntity(name, desc, email, nil)
if err != nil {
panic(err)
}
// serialize the private key
pkbuf := bytes.NewBuffer(nil)
err = ent.SerializePrivate(pkbuf, nil)
if err != nil {
panic(err)
}
buf := bytes.NewBuffer(nil)
ewrbuf, err := armor.Encode(buf, openpgp.PrivateKeyType, nil)
if err != nil {
panic(err)
}
_, err = ewrbuf.Write(pkbuf.Bytes())
if err != nil {
panic(err)
}
ewrbuf.Close()
privkey = buf.Bytes()
// serialize the public key
pkbuf = bytes.NewBuffer(nil)
err = ent.Serialize(pkbuf)
if err != nil {
panic(err)
}
buf = bytes.NewBuffer(nil)
ewrbuf, err = armor.Encode(buf, openpgp.PublicKeyType, nil)
if err != nil {
panic(err)
}
_, err = ewrbuf.Write(pkbuf.Bytes())
if err != nil {
panic(err)
}
ewrbuf.Close()
pubkey = buf.Bytes()
// validate the public key and obtain a fingerprint from it
fp, err = LoadArmoredPubKey(pubkey)
if err != nil {
panic(err)
}
return
}
示例3: 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
}
示例4: 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
}
示例5: ExportKey
func ExportKey(key, filename string) error {
entity, err := findKey(key)
if err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
w, err := armor.Encode(f, openpgp.PublicKeyType, nil)
if err != nil {
return err
}
err = entity.Serialize(w)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return nil
}
示例6: Encrypt
// Encrypt the provided bytes for the provided encryption
// keys recipients. Returns the encrypted content bytes.
func Encrypt(d []byte, encryptionKeys *openpgp.EntityList) ([]byte, error) {
var buffer *bytes.Buffer = &bytes.Buffer{}
var armoredWriter io.WriteCloser
var cipheredWriter io.WriteCloser
var err error
// Create an openpgp armored cipher writer pointing on our
// buffer
armoredWriter, err = armor.Encode(buffer, "PGP MESSAGE", nil)
if err != nil {
return nil, NewPgpError(ERR_ENCRYPTION_ENCODING, fmt.Sprintf("Can't make armor: %v", err))
}
// Create an encrypted writer using the provided encryption keys
cipheredWriter, err = openpgp.Encrypt(armoredWriter, *encryptionKeys, nil, nil, nil)
if err != nil {
return nil, NewPgpError(ERR_ENCRYPTION_ENCRYPT, fmt.Sprintf("Error encrypting: %v", err))
}
// Write (encrypts on the fly) the provided bytes to
// cipheredWriter
_, err = cipheredWriter.Write(d)
if err != nil {
return nil, NewPgpError(ERR_ENCRYPTION_ENCRYPT, fmt.Sprintf("Error copying encrypted content: %v", err))
}
cipheredWriter.Close()
armoredWriter.Close()
return buffer.Bytes(), nil
}
示例7: NewEntity
// NewEntity creates a new entity. It doesn't provide an option for comments.
func NewEntity(name, email, outFile string) (ne *openpgp.Entity, err error) {
ne, err = openpgp.NewEntity(name, "", email, DefaultConfig)
if err != nil {
return
}
out, err := os.Create(outFile)
if err != nil {
ne = nil
return
}
hdr := map[string]string{
"Version": fmt.Sprintf("Keybase Go client (OpenPGP version %s)", Version),
}
keyOut, err := armor.Encode(out, openpgp.PrivateKeyType, hdr)
if err != nil {
ne = nil
return
}
defer func() {
keyOut.Close()
out.Close()
}()
err = ne.SerializePrivate(keyOut, DefaultConfig)
if err != nil {
ne = nil
return
}
return
}
示例8: Close
func (d *dashEscaper) Close() (err error) {
if !d.atBeginningOfLine {
if err = d.buffered.WriteByte(lf); err != nil {
return
}
}
sig := new(packet.Signature)
sig.SigType = packet.SigTypeText
sig.PubKeyAlgo = d.privateKey.PubKeyAlgo
sig.Hash = d.hashType
sig.CreationTime = d.config.Now()
sig.IssuerKeyId = &d.privateKey.KeyId
if err = sig.Sign(d.h, d.privateKey, d.config); err != nil {
return
}
out, err := armor.Encode(d.buffered, "PGP SIGNATURE", nil)
if err != nil {
return
}
if err = sig.Serialize(out); err != nil {
return
}
if err = out.Close(); err != nil {
return
}
if err = d.buffered.Flush(); err != nil {
return
}
return
}
示例9: Export
// Export writes out the named public key, or all public keys if keyID
// is empty. The result is an ASCII-armoured public key.
func (keyRing *KeyRing) Export(keyID string) (armoured string, err error) {
buf := new(bytes.Buffer)
blockType := openpgp.PublicKeyType
blockHeaders := map[string]string{
"Version": fmt.Sprintf("Keybase Go client (OpenPGP version %s)", Version),
}
armourBuffer, err := armor.Encode(buf, blockType, blockHeaders)
if err != nil {
return
}
if keyID != "" {
e, ok := keyRing.Entities[strings.ToLower(keyID)]
if !ok {
err = ErrKeyNotFound
return
}
e.Serialize(armourBuffer)
} else {
if len(keyRing.Entities) == 0 {
err = ErrKeyNotFound
return
}
for _, e := range keyRing.Entities {
e.Serialize(armourBuffer)
}
}
armourBuffer.Close()
armoured = string(buf.Bytes())
return
}
示例10: encryptMail
func encryptMail(e Envelope, keys openpgp.EntityList) (*bytes.Buffer, error) {
var err error
var contenttype string
var buffer *bytes.Buffer
var armored io.WriteCloser
var crypter io.WriteCloser
buffer = bytes.NewBuffer(nil)
armored, err = armor.Encode(buffer, "PGP MESSAGE", nil)
if err != nil {
return buffer, err
}
crypter, err = openpgp.Encrypt(armored, keys, nil, nil, nil)
if err != nil {
return buffer, err
}
contenttype = e.Mail.Header.Get("Content-Type")
if contenttype == "" {
contenttype = "text/plain"
}
fmt.Fprintf(crypter, "Content-Type: %s\n\n", contenttype)
io.Copy(crypter, e.Mail.Body)
crypter.Close()
armored.Close()
return buffer, nil
}
示例11: encryptWith
func encryptWith(m *Message, pubkeys openpgp.EntityList, signingEntity *openpgp.Entity, passphrase string) *EncryptStatus {
if len(pubkeys) == 0 {
return createEncryptFailure("no recipient keys")
}
buffer := new(bytes.Buffer)
ar, err := armor.Encode(buffer, "PGP MESSAGE", nil)
if err != nil {
return createEncryptFailure("error encoding output message: " + err.Error())
}
if signingEntity.PrivateKey == nil {
return createEncryptFailure("signing key has no private key")
}
if signingEntity != nil && isSigningKeyLocked(signingEntity, passphrase) {
return &EncryptStatus{Code: StatusFailedPassphraseNeeded}
}
w, err := openpgp.Encrypt(ar, pubkeys, signingEntity, nil, openpgpConfig)
if err != nil {
return createEncryptFailure("encryption operation failed: " + err.Error())
}
bodyPart := createBodyMimePart(m)
w.Write(bodyPart.rawContent)
w.Close()
ar.Close()
buffer.WriteString("\n")
err = writeEncryptedMimeBody(m, buffer.Bytes())
if err != nil {
return createEncryptFailure(err.Error())
}
if signingEntity != nil {
return &EncryptStatus{Code: StatusSignedAndEncrypted, Message: m}
} else {
return &EncryptStatus{Code: StatusEncryptedOnly, Message: m}
}
}
示例12: Sign
// Sign signs the given message.
func (keyRing *KeyRing) Sign(message []byte, keyID string) (sig []byte, err error) {
err = keyRing.Unlock(keyID)
if err != nil {
return
}
signer := keyRing.Entities[strings.ToLower(keyID)]
buf := new(bytes.Buffer)
hdr := map[string]string{
"Version": fmt.Sprintf("Keybase Go client (OpenPGP version %s)", Version),
}
armourBuf, err := armor.Encode(buf, "PGP MESSAGE", hdr)
if err != nil {
return
}
opSig := &packet.OnePassSignature{
SigType: packet.SigTypeBinary,
Hash: crypto.SHA1,
PubKeyAlgo: packet.PubKeyAlgoRSA,
KeyId: signer.PrimaryKey.KeyId,
IsLast: true,
}
err = opSig.Serialize(armourBuf)
if err != nil {
return
}
literalPacket, err := newLiteralDataPacket(message, "", uint32(time.Now().Unix()))
if err != nil {
return
}
_, err = armourBuf.Write(literalPacket)
if err != nil {
return
}
sigPacket := &packet.Signature{
SigType: packet.SigTypeBinary,
IssuerKeyId: &signer.PrimaryKey.KeyId,
PubKeyAlgo: packet.PubKeyAlgoRSA,
Hash: crypto.SHA1,
CreationTime: time.Now(),
}
h := sha1.New()
h.Write(message)
err = sigPacket.Sign(h, signer.PrivateKey, nil)
if err != nil {
return
}
err = sigPacket.Serialize(armourBuf)
if err != nil {
return
}
armourBuf.Close()
sig = buf.Bytes()
return
}
示例13: main
func main() {
w, _ := armor.Encode(os.Stdout, "PGP MESSAGE", nil)
plaintext, _ := openpgp.SymmetricallyEncrypt(w, []byte("golang"), nil)
fmt.Fprintf(plaintext, "Hello from golang.\n")
plaintext.Close()
w.Close()
fmt.Print("\n")
}
示例14: WriteArmoredPackets
func WriteArmoredPackets(w io.Writer, root PacketRecord) error {
armw, err := armor.Encode(w, openpgp.PublicKeyType, nil)
defer armw.Close()
if err != nil {
return err
}
return WritePackets(armw, root)
}
示例15: armoredDetachSign
func armoredDetachSign(w io.Writer, signer *Entity, message io.Reader, sigType packet.SignatureType, config *packet.Config) (err error) {
out, err := armor.Encode(w, SignatureType, nil)
if err != nil {
return
}
err = detachSign(out, signer, message, sigType, config)
if err != nil {
return
}
return out.Close()
}