func PeekAlgorithm(buf []byte) (Algorithm, error) {
	alg := NONE
	algVal, err := PeekHeaderField(buf, "alg")
	if err != nil {
		return NONE, err
	}
	if err = (&alg).UnmarshalText([]byte(algVal)); err != nil {
		return NONE, err
	}
	return alg, nil
}
PeekAlgorithm extracts the signing algorithm listed in the "alg" field of
the serialized JWT buf's header and attempts to unmarshal it into an
Algorithm. An error will be returned if the alg field is not specified in
the JWT header, or is otherwise invalid.
package jwt
import (
	"bytes"
	"encoding/json"
	"reflect"
)
var tokenSep = []byte{'.'}
func Decode(alg Algorithm, signer Signer, buf []byte, obj interface{}) error {
	ut := UnverifiedToken{}
	if err := DecodeUnverifiedToken(buf, &ut); err != nil {
		return err
	}
	sig, err := signer.Verify(buf[:len(ut.Header)+len(tokenSep)+len(ut.Payload)], ut.Signature)
	if err != nil {
		return ErrInvalidSignature
	}
	headerBuf, err := b64.DecodeString(string(ut.Header))
	if err != nil {
		return err
	}
	header := Header{}
	if err = json.Unmarshal(headerBuf, &header); err != nil {
		return err
	}
	if alg != header.Algorithm {
		return ErrInvalidAlgorithm
	}
	if err := decodeToObjOrFieldWithTag(headerBuf, obj, "header", &header); err != nil {
		return err
	}
	payloadBuf, err := b64.DecodeString(string(ut.Payload))
	if err != nil {
		return err
	}
	payload := Claims{}
	if err := json.Unmarshal(payloadBuf, &payload); err != nil {
		return err
	}
	if err := decodeToObjOrFieldWithTag(payloadBuf, obj, "payload", &payload); err != nil {
		return err
	}
	field := getFieldWithTag(obj, "signature")
	if field != nil {
		field.Set(reflect.ValueOf(sig))
	}
	return nil
}
func Encode(alg Algorithm, signer Signer, obj interface{}) ([]byte, error) {
	headerObj, payloadObj, err := encodeTargets(alg, obj)
	if err != nil {
		return nil, err
	}
	header, err := json.Marshal(headerObj)
	if err != nil {
		return nil, err
	}
	headerEnc := make([]byte, b64.EncodedLen(len(header)))
	b64.Encode(headerEnc, header)
	payload, err := json.Marshal(payloadObj)
	if err != nil {
		return nil, err
	}
	payloadEnc := make([]byte, b64.EncodedLen(len(payload)))
	b64.Encode(payloadEnc, payload)
	var buf bytes.Buffer
	if _, err = buf.Write(headerEnc); err != nil {
		return nil, err
	}
	if _, err = buf.Write(tokenSep); err != nil {
		return nil, err
	}
	if _, err = buf.Write(payloadEnc); err != nil {
		return nil, err
	}
	sig, err := signer.Sign(buf.Bytes())
	if err != nil {
		return nil, err
	}
	if _, err = buf.Write(tokenSep); err != nil {
		return nil, err
	}
	if _, err = buf.Write(sig); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
func PeekHeaderField(buf []byte, field string) (string, error) {
	return peekField(buf, field, tokenPositionHeader)
}
func PeekPayloadField(buf []byte, field string) (string, error) {
	return peekField(buf, field, tokenPositionPayload)
}