go get github.com/mushroomsir/jwsgo
- HMAC signatures with HS256, HS384 and HS512.
- Support custom algorithm for encrypt data.
- Support custom Header
- Easy to understand and use.
NewSha256 create the jws instance with HMAC-SHA256.
// or NewSha512, NewSha384
jws:=jwsgo.NewSha256("Secret-key")
// create the payload
payload := &Payload{
Iss: "http://google.com/",
Exp: 3610,
Iat: 10,
}
// You can also add some extra fileds
payload.Set("userid", "123456")
// encode this payload and get token
token,err := jws.Encode(payload)
// decode token
playload,err := jws.Decode(token)
you can even make own Header with custom value.
header := &Header{
Algorithm: "HS1",
}
header.Set("id", "mushroom")
token, err := jws.EncodeWith(header, payload)
use custom hash func for encrypt data.
hasher := func(data string) []byte {
h := hmac.New(sha1.New, []byte("xx"))
h.Write([]byte(data))
return h.Sum(nil)
}
jws := New("HS256",hasher)