예제 #1
0
func buildSharedBox(message []byte, peers []PublicKey, btype byte) []byte {
	if message == nil {
		return nil
	}

	for _, peer := range peers {
		if peer == nil {
			return nil
		} else if !KeyIsSuitable(nil, peer) {
			return nil
		}
	}

	e_priv, e_pub, ok := GenerateKey()
	if !ok {
		return nil
	}

	shared, ok := strongbox.GenerateKey()
	if !ok {
		return nil
	}
	defer zero(shared)

	packPeers := newbw([]byte{peerList})
	packPeers.WriteUint32(uint32(len(peers)))
	for _, peer := range peers {
		packPeers.Write(peer)
		pbox, ok := boxForPeer(e_priv, peer, shared)
		if !ok {
			return nil
		}
		packPeers.Write(pbox)
	}
	plist := packPeers.Bytes()
	if plist == nil {
		return nil
	}

	packer := newbw([]byte{btype})
	packer.Write(e_pub)
	packer.Write(plist)
	sbox, ok := strongbox.Seal(message, shared)
	if !ok {
		return nil
	}
	packer.Write(sbox)
	return packer.Bytes()
}
예제 #2
0
파일: tkdf_test.go 프로젝트: postfix/tkdf
// Benchmark encryption of an 80-byte strongbox key.
func BenchmarkEncrypt(b *testing.B) {
	b.StopTimer()
	var ok bool
	bmData, ok = strongbox.GenerateKey()
	if !ok {
		fmt.Println("tkdf: failed to generate strongbox secret key.")
		b.FailNow()
	}
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		bmBox = Encrypt(testKey, bmData)
		if bmBox == nil {
			fmt.Println("tkdf: failed to encrypt benchmark")
			b.FailNow()
		}
	}
}