File size: 1,170 Bytes
4bcc2be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mlkem_test
import (
"crypto/mlkem"
"log"
)
func Example() {
// Alice generates a new key pair and sends the encapsulation key to Bob.
dk, err := mlkem.GenerateKey768()
if err != nil {
log.Fatal(err)
}
encapsulationKey := dk.EncapsulationKey().Bytes()
// Bob uses the encapsulation key to encapsulate a shared secret, and sends
// back the ciphertext to Alice.
ciphertext := Bob(encapsulationKey)
// Alice decapsulates the shared secret from the ciphertext.
sharedSecret, err := dk.Decapsulate(ciphertext)
if err != nil {
log.Fatal(err)
}
// Alice and Bob now share a secret.
_ = sharedSecret
}
func Bob(encapsulationKey []byte) (ciphertext []byte) {
// Bob encapsulates a shared secret using the encapsulation key.
ek, err := mlkem.NewEncapsulationKey768(encapsulationKey)
if err != nil {
log.Fatal(err)
}
sharedSecret, ciphertext := ek.Encapsulate()
// Alice and Bob now share a secret.
_ = sharedSecret
// Bob sends the ciphertext to Alice.
return ciphertext
}
|