Go - Golang openpg - Create Key pair and create signature - go

I'm currently working on openpgp in combination with golang. I use the following code to generate a new keypair and create a self-signature on the resulting public key:
package main
import (
"bytes"
"crypto"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"fmt"
)
//Create ASscii Armor from openpgp.Entity
func PubEntToAsciiArmor(pubEnt *openpgp.Entity) (asciiEntity string) {
gotWriter := bytes.NewBuffer(nil)
wr, errEncode := armor.Encode(gotWriter, openpgp.PublicKeyType, nil)
if errEncode != nil {
fmt.Println("Encoding Armor ", errEncode.Error())
return
}
errSerial := pubEnt.Serialize(wr)
if errSerial != nil {
fmt.Println("Serializing PubKey ", errSerial.Error())
}
errClosing := wr.Close()
if errClosing != nil {
fmt.Println("Closing writer ", errClosing.Error())
}
asciiEntity = gotWriter.String()
return
}
func main() {
var entity *openpgp.Entity
entity, err := openpgp.NewEntity("itis", "test", "itis#itis3.com", nil)
if err != nil {
fmt.Println("ERROR")
}
usrIdstring := ""
for _, uIds := range entity.Identities {
usrIdstring = uIds.Name
}
var priKey = entity.PrivateKey
var sig = new(packet.Signature)
//Prepare sign with our configs/////IS IT A MUST ??
sig.Hash = crypto.SHA1
sig.PubKeyAlgo = priKey.PubKeyAlgo
sig.CreationTime = time.Now()
dur := new(uint32)
*dur = uint32(365 * 24 * 60 * 60)
sig.SigLifetimeSecs = dur //a year
issuerUint := new(uint64)
*issuerUint = priKey.KeyId
sig.IssuerKeyId = issuerUint
sig.SigType = packet.SigTypeGenericCert
err = sig.SignKey(entity.PrimaryKey, entity.PrivateKey, nil)
if err != nil {
fmt.Println("ERROR")
}
err = sig.SignUserId(usrIdstring, entity.PrimaryKey, entity.PrivateKey, nil)
if err != nil {
fmt.Println("ERROR")
}
entity.SignIdentity(usrIdstring, entity, nil)
var copy = entity
var asciiSignedKey = PubEntToAsciiArmor(copy)
fmt.Println(asciiSignedKey)
}
1.) When I serialize the public key (to get an armored version of it), I get the following error message:
Serializing PubKey openpgp: invalid argument: Signature: need to call Sign, SignUserId or SignKey before Serialize
I thought I just used every possible way to create a signature on that key?
2.) I still receive an output from problem 1, when I upload the key to a keyserver, than the available information are incomplete. Only the key-id and the creation date are listed. All additional information like, self-signature, user-id-string and so on are missing (example: https://pgp.mit.edu/pks/lookup?search=0xbe6ee21e94a73ba5&op=index). What went wrong? Is it related to error 1?
PS: I am new to golang, started today.

Maybe this will do what you want. Disclaimer: I am not an expert in openpgp; I don't know whether this is correct or not. But it does work with gpg --import.
package main
import (
"fmt"
"os"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
)
func main() {
var e *openpgp.Entity
e, err := openpgp.NewEntity("itis", "test", "itis#itis3.com", nil)
if err != nil {
fmt.Println(err)
return
}
// Add more identities here if you wish
// Sign all the identities
for _, id := range e.Identities {
err := id.SelfSignature.SignUserId(id.UserId.Id, e.PrimaryKey, e.PrivateKey, nil)
if err != nil {
fmt.Println(err)
return
}
}
w, err := armor.Encode(os.Stdout, openpgp.PublicKeyType, nil)
if err != nil {
fmt.Println(err)
return
}
defer w.Close()
e.Serialize(w)
}

This seems to be a known issue: https://github.com/golang/go/issues/6483. The workaround is to call SerializePrivate first, even if you don't use the result.

I wrote https://github.com/alokmenghrajani/gpgeez for exactly this purpose. It's a Go library which makes things like key creating or exporting a key as an armored string easier.
Here is the gist of it, without any error checking:
func CreateKey() *openpgp.Entity {
key, _ := openpgp.NewEntity(name, comment, email, nil)
for _, id := range key.Identities {
id.SelfSignature.PreferredSymmetric = []uint8{...}
id.SelfSignature.PreferredHash = []uint8{...}
id.SelfSignature.PreferredCompression = []uint8{...}
id.SelfSignature.SignUserId(id.UserId.Id, key.PrimaryKey, key.PrivateKey, nil)
}
// Self-sign the Subkeys
for _, subkey := range key.Subkeys {
subkey.Sig.SignKey(subkey.PublicKey, key.PrivateKey, nil)
}
return r
}

Related

Ethereum error {"code":-32000,"message":"unknown account"}

I am trying to send a raw transaction with eth.sendTransaction but I am getting an error that says {"code":-32000,"message":"unknown account"}. I am not sure what is causing this and I cant seem to find an answer on the internet. Can anyone help me figure it out? Here is my code:
func ExecuteSignedTransaction(rawTransaction string) {
var hash web3.Hash
data := make(map[string]interface{})
data["data"] = rawTransaction
err := Web3HTTPClient.Call("eth_sendTransaction", &hash, data)
if err != nil{
fmt.Println(err)
Os.Exit(1)
}
fmt.Println("Sent tx hash:", hash)
}
So, what I might do here:
import (
"strings"
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)
var chainId = big.NewInt(1) // chain id for the ethereum mainnet, change according to needs
func ecdsaPrivateKeyFromHex(privKeyHex string) *ecdsa.PrivateKey {
ecdsaKey, err := crypto.HexToECDSA(privKeyHex)
if err != nil { panic(err) }
return ecdsaKey
}
func newTransactOpts(privKey *ecdsa.PrivateKey) *bind.TransactOpts {
transactOpts, err := bind.NewKeyedTransactorWithChainID(privKey, chainId)
if err != nil { panic(err) }
return transactOpts
}
func newRpcClient() *ethclient.Client {
c, err := ethclient.Dial("insert rpc url here")
if err != nil { panic(err) }
return c
}
// note: constructing the *types.Transaction object left as
// an exercise to the reader
func ExecuteTransaction(rawTransaction *types.Transaction) {
privKeyHex := "0xblahblahblahblahblah" // use your own account's private key
transactOpts := newTransactOpts(ecdsaPrivateKeyFromHex(privKeyHex))
signedTxn, err := transactOpts.Signer(transactOpts.From, rawTransaction)
if err != nil { panic(err) }
rpcClient := newRpcClient()
if err := rpcClient.SendTransaction(context.Background(), signedTxn); err != nil { panic(err) }
// do whatever
}

Read and merge two Yaml files in go language

Assuming we have two yaml files
master.yaml
someProperty: "someVaue"
anotherProperty: "anotherValue"
override.yaml
someProperty: "overriddenVaue"
Is it possible to unmarshall, merge, and then write those changes to a file without having to define a struct for every property in the yaml file?
The master file has over 500 properties in it that are not at all important to the service at this point of execution, so ideally I'd be able to just unmarshal into a map, do a merge and write out in yaml again but I'm relatively new to go so wanted some opinions.
I've got some code to read the yaml into an interface but i'm unsure on the best approach to then merge the two.
var masterYaml interface{}
yamlBytes, _ := ioutil.ReadFile("master.yaml")
yaml.Unmarshal(yamlBytes, &masterYaml)
var overrideYaml interface{}
yamlBytes, _ = ioutil.ReadFile("override.yaml")
yaml.Unmarshal(yamlBytes, &overrideYaml)
I've looked into libraries like mergo but i'm not sure if that's the right approach.
I'm hoping that after the master I would be able to write out to file with properties
someProperty: "overriddenVaue"
anotherProperty: "anotherValue"
Assuming that you just want to merge at the top level, you can unmarshal into maps of type map[string]interface{}, as follows:
package main
import (
"io/ioutil"
"gopkg.in/yaml.v2"
)
func main() {
var master map[string]interface{}
bs, err := ioutil.ReadFile("master.yaml")
if err != nil {
panic(err)
}
if err := yaml.Unmarshal(bs, &master); err != nil {
panic(err)
}
var override map[string]interface{}
bs, err = ioutil.ReadFile("override.yaml")
if err != nil {
panic(err)
}
if err := yaml.Unmarshal(bs, &override); err != nil {
panic(err)
}
for k, v := range override {
master[k] = v
}
bs, err = yaml.Marshal(master)
if err != nil {
panic(err)
}
if err := ioutil.WriteFile("merged.yaml", bs, 0644); err != nil {
panic(err)
}
}
For a broader solution (with n input files), you can use this function. I have used #robox answer to do my solution:
func ReadValues(filenames ...string) (string, error) {
if len(filenames) <= 0 {
return "", errors.New("You must provide at least one filename for reading Values")
}
var resultValues map[string]interface{}
for _, filename := range filenames {
var override map[string]interface{}
bs, err := ioutil.ReadFile(filename)
if err != nil {
log.Info(err)
continue
}
if err := yaml.Unmarshal(bs, &override); err != nil {
log.Info(err)
continue
}
//check if is nil. This will only happen for the first filename
if resultValues == nil {
resultValues = override
} else {
for k, v := range override {
resultValues[k] = v
}
}
}
bs, err := yaml.Marshal(resultValues)
if err != nil {
log.Info(err)
return "", err
}
return string(bs), nil
}
So for this example you should call it with this order:
result, _ := ReadValues("master.yaml", "overwrite.yaml")
In the case you have an extra file newFile.yaml, you could also use this function:
result, _ := ReadValues("master.yaml", "overwrite.yaml", "newFile.yaml")
DEEP MERGE TWO YAML FILES
package main
import (
"fmt"
"io/ioutil"
"sigs.k8s.io/yaml"
)
func main() {
// declare two map to hold the yaml content
base := map[string]interface{}{}
currentMap := map[string]interface{}{}
// read one yaml file
data, _ := ioutil.ReadFile("conf.yaml")
if err := yaml.Unmarshal(data, &base); err != nil {
}
// read another yaml file
data1, _ := ioutil.ReadFile("conf1.yaml")
if err := yaml.Unmarshal(data1, &currentMap); err != nil {
}
// merge both yaml data recursively
base = mergeMaps(base, currentMap)
// print merged map
fmt.Println(base)
}
func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = mergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}

emersion/go-imap - Mark message as seen

I'm trying to mark messages as seen using this IMAP protocol implementation but It's not working as intended.
I have a function that prints unseen messages and my intention is that by the end, it mark each message as seen.
package main
import (
"emailmonitor/util"
"fmt"
)
func main() {
serverGmail := util.NewServerGmail()
serverGmail.Connect()
serverGmail.Login()
serverGmail.ListUnseenMessages()
}
//-----------------------------------------
package util
import (
"io/ioutil"
"log"
"net/mail"
"net/smtp"
imap "github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)
type ServerGmail struct {
user string
pass string
erro string
cliente *client.Client
}
func NewServerGmail() *ServerGmail {
serverGmail := &ServerGmail{}
serverGmail.user = "xxxxxx#gmail.com"
serverGmail.pass = "xxxxx"
serverGmail.erro = ""
return serverGmail
}
func (serverGmail *ServerGmail) Connect() {
// Connect to server
cliente, erro := client.DialTLS("smtp.gmail.com:993", nil)
if erro != nil {
serverGmail.erro = erro.Error()
}
log.Println("Connected")
serverGmail.cliente = cliente
}
func (serverGmail *ServerGmail) Login() {
// Login
if erro := serverGmail.cliente.Login(serverGmail.user, serverGmail.pass); erro != nil {
serverGmail.erro = erro.Error()
}
log.Println("Logged")
}
func (serverGmail *ServerGmail) setLabelBox(label string) *imap.MailboxStatus {
mailbox, erro := serverGmail.cliente.Select(label, true)
if erro != nil {
serverGmail.erro = erro.Error()
}
return mailbox
}
func (serverGmail *ServerGmail) ListUnseenMessages() {
// set mailbox to INBOX
serverGmail.setLabelBox("INBOX")
// criteria to search for unseen messages
criteria := imap.NewSearchCriteria()
criteria.WithoutFlags = []string{"\\Seen"}
uids, err := serverGmail.cliente.UidSearch(criteria)
if err != nil {
log.Println(err)
}
seqSet := new(imap.SeqSet)
seqSet.AddNum(uids...)
section := &imap.BodySectionName{}
items := []imap.FetchItem{imap.FetchEnvelope, imap.FetchFlags, imap.FetchInternalDate, section.FetchItem()}
messages := make(chan *imap.Message)
go func() {
if err := serverGmail.cliente.UidFetch(seqSet, items, messages); err != nil {
log.Fatal(err)
}
}()
for message := range messages {
log.Println(message.Uid)
if message == nil {
log.Fatal("Server didn't returned message")
}
r := message.GetBody(section)
if r == nil {
log.Fatal("Server didn't returned message body")
}
// Create a new mail reader
mr, err := mail.CreateReader(r)
if err != nil {
log.Fatal(err)
}
// Print some info about the message
header := mr.Header
if date, err := header.Date(); err == nil {
log.Println("Date:", date)
}
if from, err := header.AddressList("From"); err == nil {
log.Println("From:", from)
}
if to, err := header.AddressList("To"); err == nil {
log.Println("To:", to)
}
if subject, err := header.Subject(); err == nil {
log.Println("Subject:", subject)
}
// MARK "SEEN" ------- STARTS HERE ---------
seqSet.Clear()
seqSet.AddNum(message.Uid)
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.SeenFlag}
erro := serverGmail.cliente.UidStore(seqSet, item, flags, nil)
if erro != nil {
panic("error!")
}
}
}
Link from Documentation: https://godoc.org/github.com/emersion/go-imap/client#Client.UidStore
Tried to do something similar to Store example.
What can be done to fix it?
modify the following line by changing true to false
mailbox, erro := serverGmail.cliente.Select(label, true)
once you've done this, when the message is fetched (using the UidFetch), it will be automatically marked to "Seen"

golang leveldb get snapshot error

I am get leveldb's all key-val to a map[string][]byte, but it is not running as my expection.
code is as below
package main
import (
"fmt"
"strconv"
"github.com/syndtr/goleveldb/leveldb"
)
func main() {
db, err := leveldb.OpenFile("db", nil)
if err != nil {
panic(err)
}
defer db.Close()
for i := 0; i < 10; i++ {
err := db.Put([]byte("key"+strconv.Itoa(i)), []byte("value"+strconv.Itoa(i)), nil)
if err != nil {
panic(err)
}
}
snap, err := db.GetSnapshot()
if err != nil {
panic(err)
}
if snap == nil {
panic("snap shot is nil")
}
data := make(map[string][]byte)
iter := snap.NewIterator(nil, nil)
for iter.Next() {
Key := iter.Key()
Value := iter.Value()
data[string(Key)] = Value
}
iter.Release()
if iter.Error() != nil {
panic(iter.Error())
}
for k, v := range data {
fmt.Println(string(k) + ":" + string(v))
}
}
but the result is below
key3:value9
key6:value9
key7:value9
key8:value9
key1:value9
key2:value9
key4:value9
key5:value9
key9:value9
key0:value9
rather not key0:value0
Problem is with casting around types (byte[] to string, etc.).
You are trying to print string values. To avoid unnecessary casting apply the following modifications:
Change data initialization into data := make(map[string]string)
Assign values into data with `data[string(Key)] = string(Value) (by the way, don't use capitalization for variables you aren't intend to export)
Print data's values with fmt.Println(k + ":" + v))
This should produce the following result:
key0:value0
key1:value1
key7:value7
key2:value2
key3:value3
key4:value4
key5:value5
key6:value6
key8:value8
key9:value9

flag redefined - panic searching by key in YouTube data API 3

I am trying to search YouTube video by key like in the golang example. I modified that code a little to let it search by different keys several times.
When I search once it is ok.
func main() {
result1, err1 := SearchYoutubeByKey("hello")
if err1 != nil {
panic(err1)
}
fmt.Println(result1)
// result2, err2 := SearchYoutubeByKey("world")
// if err2 != nil {
// panic(err2)
// }
// fmt.Println(result2)
}
But if I search twice ...
func main() {
result1, err1 := SearchYoutubeByKey("hello")
if err1 != nil {
panic(err1)
}
fmt.Println(result1)
result2, err2 := SearchYoutubeByKey("world")
if err2 != nil {
panic(err2)
}
fmt.Println(result2)
}
... then it panics with error message ...
flag redefined: query
... on line ...
query := flag.String("query", str, "Search term")
Full code:
package main
import (
"code.google.com/p/google-api-go-client/googleapi/transport"
"code.google.com/p/google-api-go-client/youtube/v3"
"flag"
"fmt"
"net/http"
)
var (
maxResults = flag.Int64("max-results", 25, "Max YouTube results")
service *youtube.Service
response *youtube.SearchListResponse
)
const developerKey = "youtube developer key"
type YoutubeSearchResult struct {
Title, YoutubeId string
}
func SearchYoutubeByKey(str string) (result []*YoutubeSearchResult, err error) {
query := flag.String("query", str, "Search term")
flag.Parse()
client := &http.Client{
Transport: &transport.APIKey{Key: developerKey},
}
service, err = youtube.New(client)
if err != nil {
return
}
// Make the API call to YouTube.
call := service.Search.List("id,snippet").
Q(*query).
MaxResults(*maxResults)
response, err = call.Do()
if err != nil {
return
}
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Id.Kind {
case "youtube#video":
result = append(result, &YoutubeSearchResult{Title: item.Snippet.Title, YoutubeId: item.Id.VideoId})
}
}
return
}
func main() {
result1, err1 := SearchYoutubeByKey("hello")
if err1 != nil {
panic(err1)
}
fmt.Println(result1)
result2, err2 := SearchYoutubeByKey("world")
if err2 != nil {
panic(err2)
}
fmt.Println(result2)
}
So it is impossible to use this code on a website. Only the first user will be able to search first time, the others will fail.
I cannot change flag during runtime but how to search by 2 different keys in one program?
Update
working solution:
package main
import (
"code.google.com/p/google-api-go-client/googleapi/transport"
"code.google.com/p/google-api-go-client/youtube/v3"
"flag"
"fmt"
"net/http"
)
var (
maxResults = flag.Int64("max-results", 25, "Max YouTube results")
service *youtube.Service
response *youtube.SearchListResponse
query = flag.String("query", "str", "Search term")
)
const developerKey = "youtube api key"
type YoutubeSearchResult struct {
Title, YoutubeId string
}
func SearchYoutubeByKey(str string) (result []*YoutubeSearchResult, err error) {
flag.Parse()
client := &http.Client{
Transport: &transport.APIKey{Key: developerKey},
}
service, err = youtube.New(client)
if err != nil {
return
}
// Make the API call to YouTube.
call := service.Search.List("id,snippet").
Q(str).
MaxResults(*maxResults)
response, err = call.Do()
if err != nil {
return
}
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Id.Kind {
case "youtube#video":
result = append(result, &YoutubeSearchResult{Title: item.Snippet.Title, YoutubeId: item.Id.VideoId})
}
}
return
}
func main() {
result1, err1 := SearchYoutubeByKey("hello")
if err1 != nil {
panic(err1)
}
fmt.Println(result1)
result2, err2 := SearchYoutubeByKey("world")
if err2 != nil {
panic(err2)
}
fmt.Println(result2)
}
The panic message tells you exactly what's wrong. Your command line flags should be defined only once. If you try to redefine them during runtime, it panics.

Resources