本文整理汇总了Golang中github.com/lestrrat/go-jwx/buffer.Buffer函数的典型用法代码示例。如果您正苦于以下问题:Golang Buffer函数的具体用法?Golang Buffer怎么用?Golang Buffer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Buffer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewRawKeyFromPublicKey
func NewRawKeyFromPublicKey(pubkey *ecdsa.PublicKey) *rawkey {
r := &rawkey{}
r.Curve = curve{pubkey.Curve}
r.X = buffer.Buffer(pubkey.X.Bytes())
r.Y = buffer.Buffer(pubkey.Y.Bytes())
return r
}
示例2: Serialize
// Serialize converts the mssage into a compact JSON format
func (s CompactSerialize) Serialize(m *Message) ([]byte, error) {
if len(m.Signatures) != 1 {
return nil, errors.New("wrong number of signatures for compact serialization")
}
signature := m.Signatures[0]
hdr := NewHeader()
if err := hdr.Copy(signature.ProtectedHeader.Header); err != nil {
return nil, err
}
hdr, err := hdr.Merge(signature.PublicHeader)
if err != nil {
return nil, err
}
hdrbuf, err := hdr.Base64Encode()
if err != nil {
return nil, err
}
b64payload, err := m.Payload.Base64Encode()
if err != nil {
return nil, err
}
b64signature, err := buffer.Buffer(signature.Signature).Base64Encode()
if err != nil {
return nil, err
}
buf := append(append(append(append(hdrbuf, '.'), b64payload...), '.'), b64signature...)
return buf, nil
}
示例3: NewRsaPublicKey
// NewRsaPublicKey creates a new JWK using the given key
func NewRsaPublicKey(pk *rsa.PublicKey) (*RsaPublicKey, error) {
k := &RsaPublicKey{
EssentialHeader: &EssentialHeader{KeyType: "RSA"},
N: buffer.Buffer(pk.N.Bytes()),
E: buffer.FromUint(uint64(pk.E)),
}
return k, nil
}
示例4: Base64Encode
func (h Header) Base64Encode() ([]byte, error) {
b, err := json.Marshal(h)
if err != nil {
return nil, err
}
return buffer.Buffer(b).Base64Encode()
}
示例5: NewRsaPrivateKey
// NewRsaPrivateKey creates a new JWK using the given key
func NewRsaPrivateKey(pk *rsa.PrivateKey) (*RsaPrivateKey, error) {
if len(pk.Primes) < 2 {
return nil, errors.New("two primes required for RSA private key")
}
pub, err := NewRsaPublicKey(&pk.PublicKey)
if err != nil {
return nil, err
}
k := &RsaPrivateKey{
RsaPublicKey: pub,
D: buffer.Buffer(pk.D.Bytes()),
P: buffer.Buffer(pk.Primes[0].Bytes()),
Q: buffer.Buffer(pk.Primes[1].Bytes()),
}
return k, nil
}
示例6: New
func New(hash crypto.Hash, alg, Z, apu, apv, pubinfo, privinfo []byte) *KDF {
algbuf := buffer.Buffer(alg).NData()
apubuf := buffer.Buffer(apu).NData()
apvbuf := buffer.Buffer(apv).NData()
concat := make([]byte, len(algbuf)+len(apubuf)+len(apvbuf)+len(pubinfo)+len(privinfo))
n := copy(concat, algbuf)
n += copy(concat[n:], apubuf)
n += copy(concat[n:], apvbuf)
n += copy(concat[n:], pubinfo)
n += copy(concat[n:], privinfo)
return &KDF{
hash: hash.New(),
otherinfo: concat,
round: 1,
z: Z,
}
}
示例7: MarshalJSON
func (e EncodedHeader) MarshalJSON() ([]byte, error) {
buf, err := json.Marshal(e.Header)
if err != nil {
return nil, err
}
buf, err = buffer.Buffer(buf).Base64Encode()
if err != nil {
return nil, err
}
return json.Marshal(string(buf))
}
示例8: Base64Encode
// Base64Encode creates the base64 encoded version of the JSON
// representation of this header
func (e EncodedHeader) Base64Encode() ([]byte, error) {
buf, err := json.Marshal(e.Header)
if err != nil {
return nil, err
}
buf, err = buffer.Buffer(buf).Base64Encode()
if err != nil {
return nil, err
}
return buf, nil
}
示例9: NewRawKeyFromPrivateKey
func NewRawKeyFromPrivateKey(privkey *rsa.PrivateKey) *rawkey {
r := NewRawKeyFromPublicKey(&privkey.PublicKey)
r.D = buffer.Buffer(privkey.D.Bytes())
r.P = buffer.Buffer(privkey.Primes[0].Bytes())
r.Q = buffer.Buffer(privkey.Primes[1].Bytes())
r.Dp = buffer.Buffer(privkey.Precomputed.Dp.Bytes())
r.Dq = buffer.Buffer(privkey.Precomputed.Dq.Bytes())
r.Qi = buffer.Buffer(privkey.Precomputed.Qinv.Bytes())
return r
}
示例10: parseCompact
// parseCompact parses a JWS value serialized via compact serialization.
func parseCompact(buf []byte) (*Message, error) {
parts := bytes.Split(buf, []byte{'.'})
if len(parts) != 3 {
return nil, ErrInvalidCompactPartsCount
}
enc := base64.RawURLEncoding
hdrbuf, err := buffer.FromBase64(parts[0])
if err != nil {
return nil, err
}
debug.Printf("hdrbuf = %s", hdrbuf.Bytes())
hdr := &EncodedHeader{Header: NewHeader()}
if err := json.Unmarshal(hdrbuf.Bytes(), hdr.Header); err != nil {
return nil, err
}
hdr.Source = hdrbuf
payload, err := buffer.FromBase64(parts[1])
if err != nil {
return nil, err
}
signature := make([]byte, enc.DecodedLen(len(parts[2])))
if _, err := enc.Decode(signature, parts[2]); err != nil {
return nil, err
}
signature = bytes.TrimRight(signature, "\x00")
s := NewSignature()
s.Signature = signature
s.ProtectedHeader = hdr
m := &Message{
Payload: buffer.Buffer(payload),
Signatures: []Signature{*s},
}
return m, nil
}
示例11: TestMultiSigner
func TestMultiSigner(t *testing.T) {
rsakey, err := rsa.GenerateKey(rand.Reader, 2048)
if !assert.NoError(t, err, "RSA key generated") {
return
}
dsakey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if !assert.NoError(t, err, "ECDSA key generated") {
return
}
ms := &MultiSign{}
s1, err := NewRsaSign(jwa.RS256, rsakey)
if !assert.NoError(t, err, "RSA Signer created") {
return
}
s1.PublicHeaders().Set("kid", "2010-12-29")
ms.AddSigner(s1)
s2, err := NewEcdsaSign(jwa.ES256, dsakey)
if !assert.NoError(t, err, "DSA Signer created") {
return
}
s2.PublicHeaders().Set("kid", "e9bc097a-ce51-4036-9562-d2ade882db0d")
ms.AddSigner(s2)
v := strings.Join([]string{`{"iss":"joe",`, ` "exp":1300819380,`, ` "http://example.com/is_root":true}`}, "\r\n")
m, err := ms.Sign(buffer.Buffer(v))
if !assert.NoError(t, err, "MultiSign succeeded") {
return
}
jsonbuf, _ := json.MarshalIndent(m, "", " ")
t.Logf("%s", jsonbuf)
}
示例12: Set
// Set sets the value of the given key to the given value. If it's
// one of the known keys, it will be set in EssentialHeader field.
// Otherwise, it is set in PrivateParams field.
func (h *Header) Set(key string, value interface{}) error {
switch key {
case "alg":
var v jwa.KeyEncryptionAlgorithm
s, ok := value.(string)
if ok {
v = jwa.KeyEncryptionAlgorithm(s)
} else {
v, ok = value.(jwa.KeyEncryptionAlgorithm)
if !ok {
return ErrInvalidHeaderValue
}
}
h.Algorithm = v
case "apu":
var v buffer.Buffer
switch value.(type) {
case buffer.Buffer:
v = value.(buffer.Buffer)
case []byte:
v = buffer.Buffer(value.([]byte))
case string:
v = buffer.Buffer(value.(string))
default:
return ErrInvalidHeaderValue
}
h.AgreementPartyUInfo = v
case "apv":
var v buffer.Buffer
switch value.(type) {
case buffer.Buffer:
v = value.(buffer.Buffer)
case []byte:
v = buffer.Buffer(value.([]byte))
case string:
v = buffer.Buffer(value.(string))
default:
return ErrInvalidHeaderValue
}
h.AgreementPartyVInfo = v
case "enc":
var v jwa.ContentEncryptionAlgorithm
s, ok := value.(string)
if ok {
v = jwa.ContentEncryptionAlgorithm(s)
} else {
v, ok = value.(jwa.ContentEncryptionAlgorithm)
if !ok {
return ErrInvalidHeaderValue
}
}
h.ContentEncryption = v
case "cty":
v, ok := value.(string)
if !ok {
return ErrInvalidHeaderValue
}
h.ContentType = v
case "epk":
v, ok := value.(*jwk.EcdsaPublicKey)
if !ok {
return ErrInvalidHeaderValue
}
h.EphemeralPublicKey = v
case "kid":
v, ok := value.(string)
if !ok {
return ErrInvalidHeaderValue
}
h.KeyID = v
case "typ":
v, ok := value.(string)
if !ok {
return ErrInvalidHeaderValue
}
h.Type = v
case "x5t":
v, ok := value.(string)
if !ok {
return ErrInvalidHeaderValue
}
h.X509CertThumbprint = v
case "x5t#256":
v, ok := value.(string)
if !ok {
return ErrInvalidHeaderValue
}
h.X509CertThumbprintS256 = v
case "x5c":
v, ok := value.([]string)
if !ok {
return ErrInvalidHeaderValue
}
h.X509CertChain = v
case "crit":
v, ok := value.([]string)
if !ok {
//.........这里部分代码省略.........
示例13: NewRawKeyFromPublicKey
func NewRawKeyFromPublicKey(pubkey *rsa.PublicKey) *rawkey {
r := &rawkey{}
r.N = buffer.Buffer(pubkey.N.Bytes())
r.E = buffer.FromUint(uint64(pubkey.E))
return r
}
示例14: Sign
// Sign takes a payload, and creates a JWS signed message.
func (m *MultiSign) Sign(payload []byte) (*Message, error) {
encoded, err := buffer.Buffer(payload).Base64Encode()
if err != nil {
return nil, err
}
msg := &Message{
Payload: buffer.Buffer(payload),
Signatures: []Signature{},
}
for _, signer := range m.Signers {
protected, err := NewHeader().Merge(signer.PublicHeaders())
if err != nil {
return nil, err
}
protected, err = protected.Merge(signer.ProtectedHeaders())
if err != nil {
return nil, err
}
protected.Algorithm = signer.SignatureAlgorithm()
protbuf, err := protected.Base64Encode()
if err != nil {
return nil, err
}
siv := append(append(protbuf, '.'), encoded...)
sigbuf, err := signer.PayloadSign(siv)
if err != nil {
return nil, err
}
hdr, err := NewHeader().Merge(signer.PublicHeaders())
if err != nil {
return nil, err
}
/*
if hdr.KeyID == "" {
if protected.KeyID != "" {
// Use the JWK in the protected field...
hdr.KeyID = protected.KeyID
} else if signer.Kid() != "" {
// Or, get it from the signer
hdr.KeyID = signer.Kid()
}
}
*/
sig := Signature{
PublicHeader: hdr,
ProtectedHeader: &EncodedHeader{Header: protected},
Signature: buffer.Buffer(sigbuf),
}
msg.Signatures = append(msg.Signatures, sig)
}
return msg, nil
}
示例15: 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
}
}