本文整理匯總了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"))