當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Crypto.Decrypt方法代碼示例

本文整理匯總了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
}
開發者ID:rakutentech,項目名稱:gorouter,代碼行數:28,代碼來源:signature.go

示例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"))
開發者ID:idouba,項目名稱:gorouter,代碼行數:31,代碼來源:crypto_test.go


注:本文中的github.com/cloudfoundry/gorouter/common/secure.Crypto.Decrypt方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。