Last active
November 23, 2018 00:15
-
-
Save paulc/895dd1ba7557ef5abb77bfc44a4eb95e to your computer and use it in GitHub Desktop.
Go stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/gob" | |
"encoding/hex" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"code.google.com/p/go.crypto/nacl/box" | |
"github.com/spf13/cobra" | |
) | |
type KeyPair struct { | |
PrivateKey [32]byte | |
PublicKey [32]byte | |
} | |
func HexBytes(data []byte, chunk int, sep string) string { | |
result := []string{} | |
if chunk == 0 { | |
chunk = len(data) | |
} | |
for i := 0; i < len(data); i += chunk { | |
end := i + chunk | |
if end > len(data) { | |
end = len(data) | |
} | |
result = append(result, hex.EncodeToString(data[i:end])) | |
} | |
return strings.Join(result, sep) | |
} | |
func GenerateKeys(generateJson bool) { | |
urandom, err := os.Open("/dev/urandom") | |
if err != nil { | |
log.Fatalf("Unable to open /dev/urandom [%s]\n", err) | |
} | |
publicKey, privateKey, err := box.GenerateKey(urandom) | |
if err != nil { | |
log.Fatalf("Unable to generate keys [%s]\n", err) | |
} | |
kp := KeyPair{*publicKey, *privateKey} | |
if generateJson { | |
jsonKP, err := json.MarshalIndent(kp, "", " ") | |
if err != nil { | |
log.Fatalf("Unable to generate PK [%s]\n", err) | |
} | |
os.Stdout.Write(jsonKP) | |
} else { | |
enc := gob.NewEncoder(os.Stdout) | |
err := enc.Encode(kp) | |
if err != nil { | |
log.Fatalf("Unable to encode KP [%s]\n", err) | |
} | |
/* | |
fmt.Printf("PrivateKey: %s\nPublicKey: %s\n", | |
HexBytes(privateKey[:], 4, "-"), | |
HexBytes(publicKey[:], 4, "-")) | |
*/ | |
} | |
} | |
func ParseKey(key string) (result [32]byte, err error) { | |
keyBytes, err := hex.DecodeString(strings.Replace(key, "-", "", -1)) | |
if err != nil { | |
return result, err | |
} | |
if len(keyBytes) != 32 { | |
return result, errors.New("Key must be 32 bytes") | |
} | |
for i, v := range keyBytes { | |
result[i] = v | |
} | |
return result, nil | |
} | |
func Usage() { | |
fmt.Printf("Usage\n") | |
} | |
var generateJsonFlag bool | |
var secretboxEncryptFlag bool | |
var secretboxKey string | |
func main() { | |
BoxCmd := &cobra.Command{ | |
Use: "box", | |
Run: func(cmd *cobra.Command, args []string) { | |
Usage() | |
}, | |
} | |
generateCmd := &cobra.Command{ | |
Use: "generate", | |
Short: "Generate Keys", | |
Run: func(cmd *cobra.Command, args []string) { | |
GenerateKeys(generateJsonFlag) | |
}, | |
} | |
generateCmd.Flags().BoolVarP(&generateJsonFlag, "json", "", false, "Generate JSON") | |
BoxCmd.AddCommand(generateCmd) | |
BoxCmd.Execute() | |
/* | |
generate := flag.Bool("generate", false, "Generate Keys") | |
generate := flag.Bool("", false, "Generate Keys") | |
//encrypt := flag.Bool("encrypt", false, "Encrypt") | |
//data := flag.String("data","","Data") | |
//dataHex := flag.String("dataHex","","Data (hex)") | |
privateKeyStr := flag.String("privateKey", "", "Private Key") | |
publicKeyStr := flag.String("publicKey", "", "Public Key (recipient)") | |
flag.Parse() | |
if *generate { | |
GenerateKeys() | |
} else { | |
privateKey, err := ParseKey(*privateKeyStr) | |
if err != nil { | |
log.Fatalf("Error parsing private key [%v]\n", err) | |
} | |
publicKey, err := ParseKey(*publicKeyStr) | |
if err != nil { | |
log.Fatalf("Error parsing public key [%v]\n", err) | |
} | |
fmt.Printf("PrivateKey: %s\n", HexBytes(privateKey[:], 4, "-")) | |
fmt.Printf("PublicKey: %s\n", HexBytes(publicKey[:], 4, "-")) | |
} | |
*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/hex" | |
"fmt" | |
"strings" | |
) | |
func HexBytes(data []byte, chunk int, sep string) string { | |
result := []string{} | |
if chunk == 0 { | |
chunk = len(data) | |
} | |
for i := 0; i < len(data); i += chunk { | |
end := i + chunk | |
if end > len(data) { | |
end = len(data) | |
} | |
result = append(result, hex.EncodeToString(data[i:end])) | |
} | |
return strings.Join(result, sep) | |
} | |
func UnHex(s string) ([]byte, error) { | |
return hex.DecodeString(strings.Replace(s, "-", "", -1)) | |
} | |
func main() { | |
b, _ := UnHex("abcd-1234-5678") | |
fmt.Printf(">>> %v\n", b) | |
fmt.Printf(">>> %s\n", HexBytes(b, 4, "-")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment