本文整理匯總了Golang中code/google/com/p/go/crypto/nacl/box.GenerateKey函數的典型用法代碼示例。如果您正苦於以下問題:Golang GenerateKey函數的具體用法?Golang GenerateKey怎麽用?Golang GenerateKey使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GenerateKey函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestCrypto
func TestCrypto(t *testing.T) {
obfuscationKeyword := "obfuscate"
var dummyKey [32]byte
for i := 0; i < 100; i++ {
senderPublicKey, senderPrivateKey, _ := box.GenerateKey(rand.Reader)
recipientPublicKey, recipientPrivateKey, _ := box.GenerateKey(rand.Reader)
senderCrypto := New(*senderPublicKey, dummyKey)
relayCrypto := New(*recipientPublicKey, *senderPrivateKey)
recipientCrypto := New(dummyKey, *recipientPrivateKey)
payload := make([]byte, mrand.Intn(250))
rand.Read(payload)
//sender
encrypted, _ := senderCrypto.Encrypt(payload)
obfuscated, _ := senderCrypto.Obfuscate(encrypted, obfuscationKeyword)
//relay
deobfuscated, _ := senderCrypto.Deobfuscate(obfuscated, obfuscationKeyword)
decrypted, _ := relayCrypto.Decrypt(deobfuscated)
encrypted, _ = relayCrypto.Encrypt(decrypted)
//recepient
decrypted, _ = recipientCrypto.Decrypt(encrypted)
if !bytes.Equal(payload, decrypted) {
t.Fatalf("decrypted payload is not equal to the original!")
}
}
}
示例2: main
func main() {
message := []byte("Gophers of the world, unite!")
alicePublic, alicePrivate, err := box.GenerateKey(rand.Reader)
if err != nil {
fmt.Printf("Failed to generate keypair for Alice: %v\n", err)
return
}
bobPublic, bobPrivate, err := box.GenerateKey(rand.Reader)
if err != nil {
fmt.Printf("Failed to generate keypair for Bob: %v\n", err)
return
}
encrypted := box.Seal(nil, message, &nonce, bobPublic, alicePrivate)
decrypted, ok := box.Open(nil, encrypted, &nonce, alicePublic, bobPrivate)
if !ok {
fmt.Println("Decryption failed.\n")
return
}
if !bytes.Equal(message, decrypted) {
fmt.Println("Message recovered failed.\n")
return
}
// Nonce should only be used once.
updateNonce(&nonce)
fmt.Println("OK")
}
示例3: main
func main() {
var rs [256][4][]byte
for i := range rs {
pk1, _, err := box.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
_, sk2, err := box.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
var n [24]byte // zero nonce
m := make([]byte, i)
if _, err := io.ReadFull(rand.Reader, m[:]); err != nil {
panic(err)
}
b := box.Seal(nil, m, &n, pk1, sk2)
rs[i][0] = pk1[:]
rs[i][1] = sk2[:]
rs[i][2] = m
rs[i][3] = b
}
out, err := json.MarshalIndent(rs, "", "")
if err != nil {
panic(err)
}
fmt.Print("module.exports = ")
fmt.Print(string(out))
fmt.Println(";")
}
示例4: main
func main() {
var rs [256][5][]byte
for i := range rs {
pk1, sk1, err := box.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
pk2, sk2, err := box.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
var out1, out2 [32]byte
curve25519.ScalarMult(&out1, sk1, pk2)
curve25519.ScalarMult(&out2, sk2, pk1)
if out1 != out2 {
panic("differ")
}
rs[i][0] = pk1[:]
rs[i][1] = sk1[:]
rs[i][2] = pk2[:]
rs[i][3] = sk2[:]
rs[i][4] = out1[:]
}
out, err := json.MarshalIndent(rs, "", "")
if err != nil {
panic(err)
}
fmt.Print("module.exports = ")
fmt.Print(string(out))
fmt.Println(";")
}
示例5: serverHandshake
func (c *Conn) serverHandshake() error {
spk, ssk, err := box.GenerateKey(rand.Reader)
if err != nil {
return err
}
// key exchange
cpk := new([32]byte)
_, err = c.conn.Read(cpk[:]) // recv client public key
if err != nil {
return err
}
_, err = c.conn.Write(spk[:]) // send server public key to client
if err != nil {
return err
}
// end key exchange
copy(c.id[:], cpk[:])
c.sr = NewSecureReader(c.conn, ssk, cpk)
c.sw = NewSecureWriter(c.conn, ssk, cpk)
c.handshakeComplete = true
return nil
}
示例6: clientHandshake
func (c *Conn) clientHandshake() error {
cpk, csk, err := box.GenerateKey(rand.Reader)
if err != nil {
return err
}
// key exchange
_, err = c.conn.Write(cpk[:]) // send client public key to server
if err != nil {
return fmt.Errorf("sending public key %s", err)
}
spk := new([32]byte)
_, err = io.ReadFull(c.conn, spk[:]) // recv server public key
if err != nil {
return fmt.Errorf("recv public key %s", err)
}
// end serverKey exchagne
copy(c.id[:], spk[:])
c.sr = NewSecureReader(c.conn, csk, spk)
c.sw = NewSecureWriter(c.conn, csk, spk)
c.handshakeComplete = true
return nil
}
示例7: GenerateKey
// GenerateKey generates a new key pair.
func GenerateKey() (*KeyPair, error) {
var key KeyPair
pub, priv, err := box.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
key.Public = PublicKey(*pub)
key.Private = *priv
return &key, nil
}
示例8: GenUserKeyPair
func (cc *Client) GenUserKeyPair() {
pub, prv, err := box.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
cc.PubKey = pub[:]
cc.PrvKey = prv[:]
settings.PutBits("config", "public-key", cc.PubKey)
settings.PutBits("config", "private-key", cc.PrvKey)
}
示例9: TestGenerateNodeID
func TestGenerateNodeID(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
pub, _, err := box.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
if id := generateMeshID(pub[:]); !validateMeshID(id) {
t.Errorf("Did not generate a valid MeshID")
}
}
示例10: Encrypt
func (cr *Crypto) Encrypt(data []byte) ([]byte, error) {
ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
ciphertext := box.Seal(nil, data, &cr.nonce, &cr.publicKey, ephemeralPrivateKey)
output := make([]byte, 32+len(ciphertext))
copy(output[0:32], ephemeralPublicKey[0:32])
copy(output[32:], ciphertext)
return output, nil
}
示例11: Dial
// The connection is lazily established, letting data be included in the handshake.
// A subsequent Read() or Write() may fail establishing a connection.
func Dial(addr *net.UDPAddr) (c *CurveCPConn, err error) {
c = new(CurveCPConn)
c.client = new(curveCPClient)
c.ephPublicKey, c.ephPrivateKey, err = box.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
// TODO: fetch server and client long-term keys
var sPublicKey [32]byte
var cPrivateKey [32]byte
box.Precompute(&c.client.sharedHelloKey, &sPublicKey, c.ephPrivateKey)
box.Precompute(&c.client.sharedVouchKey, &sPublicKey, &cPrivateKey)
nonceInt, err := rand.Int(rand.Reader, big.NewInt(1<<48)) // start incrementing at random [0,2^48)
if err != nil {
return nil, err
}
c.nonce = nonceInt.Int64()
c.conn, err = net.DialUDP("udp", nil, addr)
if err != nil {
return nil, err
}
c.sendHello()
deadline := 1000 // TODO: add to connection struct
connectionTimeout := time.NewTimer(min(deadline, 60*time.Second))
cookies := make(chan bool)
go c.cookieReceiver(cookies)
for {
select {
case <-cookies:
break
case <-time.After(time.Second): // repeat Hello; TODO: fuzz + backoff
c.sendHello()
case <-connectionTimeout.C:
return nil, ConnectionTimeoutError
}
}
go c.clientReactor()
return c, nil
}
示例12: TestLoadKeys
func TestLoadKeys(t *testing.T) {
in, err := ioutil.ReadFile(signerFile)
checkError(t, err)
signer, err = x509.ParsePKCS1PrivateKey(in)
checkError(t, err)
pub, priv, err := box.GenerateKey(rand.Reader)
checkError(t, err)
testPriv = make([]byte, 32)
testPub = make([]byte, 32)
copy(testPriv, priv[:])
copy(testPub, pub[:])
}
示例13: main
func main() {
pubkey, privkey, _ := box.GenerateKey(rand.Reader)
var publicKey, privateKey []byte
publicKey = (*pubkey)[0:32]
privateKey = (*privkey)[0:32]
m := map[string]string{"publicKey": base64.StdEncoding.EncodeToString(publicKey),
"privateKey": base64.StdEncoding.EncodeToString(privateKey)}
j, _ := json.Marshal(m)
fmt.Printf(string(j))
return
}
示例14: main
func main() {
armour := flag.Bool("a", false, "armour key")
outFile := flag.String("o", "signer", "output file base name")
flag.Parse()
if *outFile == "" {
log.Fatal("no output base filename specified")
}
pub, priv, err := box.GenerateKey(rand.Reader)
if err != nil {
log.Fatalf("%v", err)
}
dumpPrivate(priv, *outFile, *armour)
dumpPublic(pub, *outFile, *armour)
}
示例15: Serve
// Serve starts a secure echo server on the given listener.
func Serve(l net.Listener) error {
myPub, myPriv, err := box.GenerateKey(rand.Reader)
if err != nil {
return err
}
for {
conn, err := l.Accept()
if err != nil {
return err
}
go handle(myPub, myPriv, conn)
}
return nil
}