Skip to content

Instantly share code, notes, and snippets.

@DaveAppleton
Created April 2, 2015 21:18
Show Gist options
  • Save DaveAppleton/5a908e27d0cc568fbc23 to your computer and use it in GitHub Desktop.
Save DaveAppleton/5a908e27d0cc568fbc23 to your computer and use it in GitHub Desktop.
Sample for GoSodium - which seems to encrypt but not decrypt - need help.
package main
import (
"github.com/jasonmccampbell/GoSodium"
"github.com/jasonmccampbell/GoSodium/sodium"
"fmt"
)
func main() {
alice_pk := gosodium.AllocPublicKey()
alice_sk := gosodium.AllocSecretKey()
bob_pk := gosodium.AllocPublicKey()
bob_sk := gosodium.AllocSecretKey()
nonce := gosodium.NewBoxNonce()
if sodium.BoxKeyPair(alice_sk, alice_pk) != 0 {
panic("alice's keys not made")
}
if sodium.BoxKeyPair(bob_sk, bob_pk) != 0 {
panic("bob's keys not made")
}
fmt.Println("ZeroBytes = ",sodium.BoxZeroBytes())
message := []byte("There is much to be said for extensive research before playing golf with strangers, especially on Sundays.");
paddedMessage := make([]byte,sodium.BoxZeroBytes(),len(message)+sodium.BoxZeroBytes())
for i := range message {
paddedMessage = append(paddedMessage, message[i])
}
fmt.Println("Message length - ", len(message))
cypherTextOut := make([]byte,len(paddedMessage))
if n := sodium.Box(cypherTextOut , paddedMessage , nonce, bob_pk, alice_sk); n != 0 {
fmt.Println("Box returned ",n)
return
}
fmt.Println ("Nonce ", nonce)
fmt.Println("Box message ",paddedMessage)
fmt.Println("Box crypto ",cypherTextOut)
messageOut := make([]byte, len(cypherTextOut))
sodium.MemZero(cypherTextOut[:sodium.BoxZeroBytes()])
fmt.Println("Box crypto (z) ",cypherTextOut)
if n := sodium.BoxOpen(messageOut,cypherTextOut,nonce,alice_pk,bob_sk); n != 0 {
fmt.Println("BoxOpen returned ",n)
return
}
fmt.Println("Box message out ",messageOut)
fmt.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment