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


Golang buffer.Buffer類代碼示例

本文整理匯總了Golang中github.com/lestrrat/go-jwx/buffer.Buffer的典型用法代碼示例。如果您正苦於以下問題:Golang Buffer類的具體用法?Golang Buffer怎麽用?Golang Buffer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Buffer類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: UnmarshalJSON

// UnmarshalJSON parses the JSON buffer into a Header
func (e *EncodedHeader) UnmarshalJSON(buf []byte) error {
	b := buffer.Buffer{}
	// base646 json string -> json object representation of header
	if err := json.Unmarshal(buf, &b); err != nil {
		return err
	}

	if err := json.Unmarshal(b.Bytes(), &e.Header); err != nil {
		return err
	}

	return nil
}
開發者ID:lestrrat,項目名稱:go-jwx,代碼行數:14,代碼來源:message.go

示例2: GetBuffer

func (h Hmap) GetBuffer(name string, consume ...bool) (buffer.Buffer, error) {
	b := buffer.Buffer{}
	v, err := h.GetString(name, consume...)
	if err != nil {
		return b, err
	}

	if err := b.Base64Decode([]byte(v)); err != nil {
		return b, err
	}

	return b, nil
}
開發者ID:nabeken,項目名稱:go-jwx,代碼行數:13,代碼來源:emap.go

示例3: parseCompact

func parseCompact(buf []byte) (*Message, error) {
	debug.Printf("Parse(Compact): buf = '%s'", buf)
	parts := bytes.Split(buf, []byte{'.'})
	if len(parts) != 5 {
		return nil, ErrInvalidCompactPartsCount
	}

	hdrbuf := buffer.Buffer{}
	if err := hdrbuf.Base64Decode(parts[0]); err != nil {
		return nil, err
	}
	debug.Printf("hdrbuf = %s", hdrbuf)

	hdr := NewHeader()
	if err := json.Unmarshal(hdrbuf, hdr); err != nil {
		return nil, err
	}

	// We need the protected header to contain the content encryption
	// algorithm. XXX probably other headers need to go there too
	protected := NewEncodedHeader()
	protected.ContentEncryption = hdr.ContentEncryption
	hdr.ContentEncryption = ""

	enckeybuf := buffer.Buffer{}
	if err := enckeybuf.Base64Decode(parts[1]); err != nil {
		return nil, err
	}

	ivbuf := buffer.Buffer{}
	if err := ivbuf.Base64Decode(parts[2]); err != nil {
		return nil, err
	}

	ctbuf := buffer.Buffer{}
	if err := ctbuf.Base64Decode(parts[3]); err != nil {
		return nil, err
	}

	tagbuf := buffer.Buffer{}
	if err := tagbuf.Base64Decode(parts[4]); err != nil {
		return nil, err
	}

	m := NewMessage()
	m.AuthenticatedData.SetBytes(hdrbuf.Bytes())
	m.ProtectedHeader = protected
	m.Tag = tagbuf
	m.CipherText = ctbuf
	m.InitializationVector = ivbuf
	m.Recipients = []Recipient{
		Recipient{
			Header:       hdr,
			EncryptedKey: enckeybuf,
		},
	}
	return m, nil
}
開發者ID:nabeken,項目名稱:go-jwx,代碼行數:58,代碼來源:jwe.go

示例4: TestEncode_HS256Compact

// TestEncode_HS256Compact tests that https://tools.ietf.org/html/rfc7515#appendix-A.1 works
func TestEncode_HS256Compact(t *testing.T) {
	const hdr = `{"typ":"JWT",` + "\r\n" + ` "alg":"HS256"}`
	const hmacKey = `AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow`
	const expected = `eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk`

	hmacKeyDecoded := buffer.Buffer{}
	hmacKeyDecoded.Base64Decode([]byte(hmacKey))

	sign, err := NewHmacSign(jwa.HS256, hmacKeyDecoded.Bytes())
	if !assert.NoError(t, err, "HmacSign created successfully") {
		return
	}

	hdrbuf, err := buffer.Buffer(hdr).Base64Encode()
	if !assert.NoError(t, err, "base64 encode successful") {
		return
	}
	payload, err := buffer.Buffer(examplePayload).Base64Encode()
	if !assert.NoError(t, err, "base64 encode successful") {
		return
	}

	signingInput := bytes.Join(
		[][]byte{
			hdrbuf,
			payload,
		},
		[]byte{'.'},
	)
	signature, err := sign.PayloadSign(signingInput)
	if !assert.NoError(t, err, "PayloadSign is successful") {
		return
	}
	sigbuf, err := buffer.Buffer(signature).Base64Encode()
	if !assert.NoError(t, err, "base64 encode successful") {
		return
	}

	encoded := bytes.Join(
		[][]byte{
			signingInput,
			sigbuf,
		},
		[]byte{'.'},
	)
	if !assert.Equal(t, expected, string(encoded), "generated compact serialization should match") {
		return
	}

	msg, err := Parse(encoded)
	if !assert.NoError(t, err, "Parsing compact encoded serialization succeeds") {
		return
	}

	hdrs := msg.Signatures[0].MergedHeaders()
	if !assert.Equal(t, hdrs.Algorithm(), jwa.HS256, "Algorithm in header matches") {
		return
	}

	v, err := NewHmacVerify(jwa.HS256, hmacKeyDecoded.Bytes())
	if !assert.NoError(t, err, "HmacVerify created") {
		return
	}
	if !assert.NoError(t, v.Verify(msg), "Verify succeeds") {
		return
	}
}
開發者ID:nabeken,項目名稱:go-jwx,代碼行數:68,代碼來源:jws_test.go


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