本文整理汇总了Golang中github.com/cloudfoundry/gorouter/common/secure.Crypto.Decrypt方法的典型用法代码示例。如果您正苦于以下问题:Golang Crypto.Decrypt方法的具体用法?Golang Crypto.Decrypt怎么用?Golang Crypto.Decrypt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudfoundry/gorouter/common/secure.Crypto
的用法示例。
在下文中一共展示了Crypto.Decrypt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SignatureFromHeaders
func SignatureFromHeaders(signatureHeader, metadataHeader string, crypto secure.Crypto) (Signature, error) {
metadata := Metadata{}
signature := Signature{}
if metadataHeader == "" {
return signature, errors.New("No metadata found")
}
metadataDecoded, err := base64.URLEncoding.DecodeString(metadataHeader)
if err != nil {
return signature, err
}
err = json.Unmarshal(metadataDecoded, &metadata)
signatureDecoded, err := base64.URLEncoding.DecodeString(signatureHeader)
if err != nil {
return signature, err
}
signatureDecrypted, err := crypto.Decrypt(signatureDecoded, metadata.Nonce)
if err != nil {
return signature, err
}
err = json.Unmarshal([]byte(signatureDecrypted), &signature)
return signature, err
}
示例2:
plainText = []byte("this is a secret message!")
cipherText []byte
nonce []byte
)
BeforeEach(func() {
var err error
cipherText, nonce, err = aesGcm.Encrypt(plainText)
Expect(err).ToNot(HaveOccurred())
Expect(cipherText).ToNot(Equal(plainText))
Expect(nonce).ToNot(BeNil())
})
Context("when using correct key and nonce", func() {
It("decrypts the cipher text", func() {
decryptedText, err := aesGcm.Decrypt(cipherText, nonce)
Expect(err).ToNot(HaveOccurred())
Expect(decryptedText).To(Equal(plainText))
})
})
Context("when using an invalid key", func() {
It("returns an error", func() {
otherKey := []byte("0123456789ABCDEF")
otherAesGcm, err := secure.NewAesGCM(otherKey)
Expect(err).ToNot(HaveOccurred())
decryptedText, err := otherAesGcm.Decrypt(cipherText, nonce)
Expect(err).To(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("authentication failed"))