golang golang gpg / openpgp加密/解密示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang golang gpg / openpgp加密/解密示例相关的知识,希望对你有一定的参考价值。
package main
import (
"bytes"
"code.google.com/p/go.crypto/openpgp"
"encoding/base64"
"io/ioutil"
"log"
"os"
)
// create gpg keys with
// $ gpg --gen-key
// ensure you correct paths and passphrase
const mySecretString = "this is so very secret!"
const prefix, passphrase = "/Users/stuart-warren/", "1234"
const secretKeyring = prefix + ".gnupg/secring.gpg"
const publicKeyring = prefix + ".gnupg/pubring.gpg"
func encTest(secretString string) (string, error) {
log.Println("Secret to hide:", secretString)
log.Println("Public Keyring:", publicKeyring)
// Read in public key
keyringFileBuffer, _ := os.Open(publicKeyring)
defer keyringFileBuffer.Close()
entityList, err := openpgp.ReadKeyRing(keyringFileBuffer)
if err != nil {
return "", err
}
// encrypt string
buf := new(bytes.Buffer)
w, err := openpgp.Encrypt(buf, entityList, nil, nil, nil)
if err != nil {
return "", err
}
_, err = w.Write([]byte(mySecretString))
if err != nil {
return "", err
}
err = w.Close()
if err != nil {
return "", err
}
// Encode to base64
bytes, err := ioutil.ReadAll(buf)
if err != nil {
return "", err
}
encStr := base64.StdEncoding.EncodeToString(bytes)
// Output encrypted/encoded string
log.Println("Encrypted Secret:", encStr)
return encStr, nil
}
func decTest(encString string) (string, error) {
log.Println("Secret Keyring:", secretKeyring)
log.Println("Passphrase:", passphrase)
// init some vars
var entity *openpgp.Entity
var entityList openpgp.EntityList
// Open the private key file
keyringFileBuffer, err := os.Open(secretKeyring)
if err != nil {
return "", err
}
defer keyringFileBuffer.Close()
entityList, err = openpgp.ReadKeyRing(keyringFileBuffer)
if err != nil {
return "", err
}
entity = entityList[0]
// Get the passphrase and read the private key.
// Have not touched the encrypted string yet
passphraseByte := []byte(passphrase)
log.Println("Decrypting private key using passphrase")
entity.PrivateKey.Decrypt(passphraseByte)
for _, subkey := range entity.Subkeys {
subkey.PrivateKey.Decrypt(passphraseByte)
}
log.Println("Finished decrypting private key using passphrase")
// Decode the base64 string
dec, err := base64.StdEncoding.DecodeString(encString)
if err != nil {
return "", err
}
// Decrypt it with the contents of the private key
md, err := openpgp.ReadMessage(bytes.NewBuffer(dec), entityList, nil, nil)
if err != nil {
return "", err
}
bytes, err := ioutil.ReadAll(md.UnverifiedBody)
if err != nil {
return "", err
}
decStr := string(bytes)
return decStr, nil
}
func main() {
encStr, err := encTest(mySecretString)
if err != nil {
log.Fatal(err)
}
decStr, err := decTest(encStr)
if err != nil {
log.Fatal(err)
}
// should be done
log.Println("Decrypted Secret:", decStr)
}
json [Golang] golang #golang #snippets中有用的片段
[ fmt ](https://golang.org/pkg/fmt/)
-------
Print any `type`(struct,maps,primitives) with the `key` name
```
fmt.Printf("\n [key] :%+v \n", [value])
```
Print Error
```
fmt.Errorf(" \nError: %s", err.Error())
```
[ log ](https://golang.org/pkg/log/)
-------------
Print any `type`(struct,maps,primitives) with the `key` name
```
log.Printf("\n [key] :%+v \n", [value])
```
[ http ](https://golang.org/pkg/log/)
-------------
http middleware
```
func [middlewareName](h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do things before request
nextMiddleware.ServeHTTP(w, r)
// Do things after request
})
}
```
{
"http Middleware declaration": {
"prefix": "md",
"body": ["func $1(${2:nextMiddleware} http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(${3:w} http.ResponseWriter, ${4:r} *http.Request) {\n\t $5\n\t nextMiddleware.ServeHTTP(${3:w}, ${4:r})\n\t $6\n\t})\n}"],
"description": "Snippet for http middleware declaration"
},
"log.Printf()": {
"prefix": "lf",
"body": [
"log.Printf(\"\\n## ${1:text} ##: %+v\\n\", ${2:value})"
],
"description": "log.Printf()"
},
"fmt.Printf()": {
"prefix": "ff",
"body": [
"fmt.Printf(\"\\n## ${1:text} ## :%+v \\n\", ${2:value})",
],
"description": "log.Printf()"
}
}
以上是关于golang golang gpg / openpgp加密/解密示例的主要内容,如果未能解决你的问题,请参考以下文章