func encrypt(plaintext, keyBytes []byte) ([]byte, error) { publicKey, err := bletchley.PEMToPublicKey(keyBytes) if err != nil { return []byte{}, err } encrypted, err := bletchley.Encrypt(publicKey, plaintext) if err != nil { return []byte{}, err } return json.Marshal(encrypted) }
var _ = Describe("Serialization of asymmetric keys", func() { var privateKey *rsa.PrivateKey BeforeEach(func() { nbits := 256 var err error privateKey, err = rsa.GenerateKey(rand.Reader, nbits) Expect(err).To(BeNil()) }) Describe("private keys", func() { It("should serialize and deserialize losslessly", func() { pemBytes := bletchley.PrivateKeyToPEM(privateKey) Expect(bletchley.PEMToPrivateKey(pemBytes)).To(Equal(privateKey)) }) }) Describe("public keys", func() { It("should serialize and deserialize losslessly", func() { publicKey := privateKey.Public().(*rsa.PublicKey) pemBytes, err := bletchley.PublicKeyToPEM(publicKey) Expect(err).NotTo(HaveOccurred()) Expect(bletchley.PEMToPublicKey(pemBytes)).To(Equal(publicKey)) }) }) })