百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jose2go

> 安全
开源

Golang(GO)实施JavaScript 对象签名和加密规格

187 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Golang(GO)实施JavaScript 对象签名和加密规格

Golang (GO) Javascript Object Signing and Encryption (JOSE) and JSON Web Token (JWT) implementation

Pure Golang (GO) library for generating, decoding and encrypting JSON Web Tokens. Zero dependency, relies only on standard library.

Supports full suite of signing, encryption and compression algorithms defined by JSON Web Algorithms as of July 4, 2014 version.

Extensively unit tested and cross tested (100+ tests) for compatibility with jose.4.j, Nimbus-JOSE-JWT, json-jwt and jose-jwt libraries.

Status

Used in production. GA ready. Current version is 1.9.

Important

v1.11 more security hardening, ECDH-ES malformed ephemeral public key (epk.x) causes nil-curve dereference panic (remote DoS)

v1.10 more security hardening, #42

v1.9 security hardening, fixed found vulnerabilities

v1.8 added experimental RSA-OAEP-384 and RSA-OAEP-512 key management algorithms

v1.7 introduced deflate decompression memory limits to avoid denial-of-service attacks aka 'deflate-bomb'. See Customizing compression section for details.

v1.6 security tuning options

v1.5 bug fix release

v1.4 changes default behavior of inserting typ=JWT header if not overriden. As of 1.4 no extra headers added by library automatically. To mimic pre 1.4 behaviour use:

go
token, err := jose.Sign(..., jose.Header("typ", "JWT"))

//or

token, err := jose.Encrypt(..., jose.Header("typ", "JWT"))

v1.3 fixed potential Invalid Curve Attack on NIST curves within ECDH key management. Upgrade strongly recommended.

v1.2 breaks jose.Decode interface by returning 3 values instead of 2.

v1.2 deprecates jose.Compress method in favor of using configuration options to jose.Encrypt, the method will be removed in next release.

Migration to v1.2

Pre v1.2 decoding:

go
payload,err := jose.Decode(token,sharedKey)

Should be updated to v1.2:

go
payload, headers, err := jose.Decode(token,sharedKey)

Pre v1.2 compression:

go
token,err := jose.Compress(payload,jose.DIR,jose.A128GCM,jose.DEF, key)

Should be update to v1.2:

go
token, err := jose.Encrypt(payload, jose.DIR, jose.A128GCM, key, jose.Zip(jose.DEF))

Supported JWA algorithms

Signing

  • HMAC signatures with HS256, HS384 and HS512.
  • RSASSA-PKCS1-V1_5 signatures with RS256, RS384 and RS512.
  • RSASSA-PSS signatures (probabilistic signature scheme with appendix) with PS256, PS384 and PS512.
  • ECDSA signatures with ES256, ES384 and ES512.
  • NONE (unprotected) plain text algorithm without integrity protection

Encryption

  • RSAES OAEP (using SHA-1 and MGF1 with SHA-1) encryption with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • RSAES OAEP 256, 384, 512 (using SHA-256, 384, 512 and MGF1 with SHA-256, 384, 512) encryption with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • RSAES-PKCS1-V1_5 encryption with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • A128KW, A192KW, A256KW encryption with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • A128GCMKW, A192GCMKW, A256GCMKW encryption with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • ECDH-ES with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW with A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
  • Direct symmetric key encryption with pre-shared key A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM and A256GCM

Compression

  • DEFLATE compression

Installation

Grab package from github

go get github.com/dvsekhvalnov/jose2go or go get -u github.com/dvsekhvalnov/jose2go to update to latest version

Import package

go
import (
	"github.com/dvsekhvalnov/jose2go"
)

Usage

Creating Plaintext (unprotected) Tokens

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	token,err := jose.Sign(payload,jose.NONE, nil)

	if(err==nil) {
		//go use token
		fmt.Printf("\nPlaintext = %v\n",token)
	}
}

Creating signed tokens

HS-256, HS-384 and HS-512

Signing with HS256, HS384, HS512 expecting []byte array key of corresponding length:

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	key := []byte{97,48,97,50,97,98,100,56,45,54,49,54,50,45,52,49,99,51,45,56,51,100,54,45,49,99,102,53,53,57,98,52,54,97,102,99}

	token,err := jose.Sign(payload,jose.HS256,key)

	if(err==nil) {
		//go use token
		fmt.Printf("\nHS256 = %v\n",token)
	}
}

RS-256, RS-384 and RS-512, PS-256, PS-384 and PS-512

Signing with RS256, RS384, RS512, PS256, PS384, PS512 expecting *rsa.PrivateKey private key of corresponding length. jose2go provides convenient utils to construct *rsa.PrivateKey instance from PEM encoded PKCS1 or PKCS8 data: Rsa.ReadPrivate([]byte) under jose2go/keys/rsa package.

go
package main

import (
	"fmt"
	"io/ioutil"
	Rsa "github.com/dvsekhvalnov/jose2go/keys/rsa"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	keyBytes,err := ioutil.ReadFile("private.key")

	if(err!=nil) {
		panic("invalid key file")
	}

	privateKey,e:=Rsa.ReadPrivate(keyBytes)

	if(e!=nil) {
		panic("invalid key format")
	}

	token,err := jose.Sign(payload,jose.RS256, privateKey)

	if(err==nil) {
		//go use token
		fmt.Printf("\nRS256 = %v\n",token)
	}
}

ES-256, ES-384 and ES-512

ES256, ES384, ES512 ECDSA signatures expecting *ecdsa.PrivateKey private elliptic curve key of corresponding length. jose2go provides convenient utils to construct *ecdsa.PrivateKey instance from PEM encoded PKCS1 or PKCS8 data: ecc.ReadPrivate([]byte) or directly from X,Y,D parameters: ecc.NewPrivate(x,y,d []byte) under jose2go/keys/ecc package.

…

Creating encrypted tokens

RSA-OAEP-512, RSA-OAEP-384, RSA-OAEP-256, RSA-OAEP and RSA1_5 key management algorithm

RSA-OAEP-512, RSA-OAEP-384, RSA-OAEP-256, RSA-OAEP and RSA1_5 key management expecting *rsa.PublicKey public key of corresponding length.

…

AES Key Wrap key management family of algorithms

AES128KW, AES192KW and AES256KW key management requires []byte array key of corresponding length

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	sharedKey :=[]byte{194,164,235,6,138,248,171,239,24,216,11,22,137,199,215,133}

	token,err := jose.Encrypt(payload,jose.A128KW,jose.A128GCM,sharedKey)

	if(err==nil) {
		//go use token
		fmt.Printf("\nA128KW A128GCM = %v\n",token)
	}
}

AES GCM Key Wrap key management family of algorithms

AES128GCMKW, AES192GCMKW and AES256GCMKW key management requires []byte array key of corresponding length

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	sharedKey :=[]byte{194,164,235,6,138,248,171,239,24,216,11,22,137,199,215,133}

	token,err := jose.Encrypt(payload,jose.A128GCMKW,jose.A128GCM,sharedKey)

	if(err==nil) {
		//go use token
		fmt.Printf("\nA128GCMKW A128GCM = %v\n",token)
	}
}

ECDH-ES and ECDH-ES with AES Key Wrap key management family of algorithms

ECDH-ES and ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW key management requires *ecdsa.PublicKey elliptic curve key of corresponding length. jose2go provides convenient utils to construct *ecdsa.PublicKey instance from PEM encoded PKCS1 X509 certificate or PKIX data: ecc.ReadPublic([]byte) or directly from X,Y parameters: ecc.NewPublic(x,y []byte)under jose2go/keys/ecc package:

…

PBES2 using HMAC SHA with AES Key Wrap key management family of algorithms

PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW key management requires string passphrase from which actual key will be derived

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	passphrase := `top secret`

	token,err := jose.Encrypt(payload,jose.PBES2_HS256_A128KW,jose.A256GCM,passphrase)

	if(err==nil) {
		//go use token
		fmt.Printf("\nPBES2_HS256_A128KW A256GCM = %v\n",token)
	}
}

DIR direct pre-shared symmetric key management

Direct key management with pre-shared symmetric keys expecting []byte array key of corresponding length:

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload :=  `{"hello": "world"}`

	sharedKey :=[]byte{194,164,235,6,138,248,171,239,24,216,11,22,137,199,215,133}

	token,err := jose.Encrypt(payload,jose.DIR,jose.A128GCM,sharedKey)

	if(err==nil) {
		//go use token
		fmt.Printf("\nDIR A128GCM = %v\n",token)
	}
}

Creating compressed & encrypted tokens

DEFLATE compression

jose2go supports optional DEFLATE compression of payload before encrypting, can be used with all supported encryption and key management algorithms:

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	payload := `{"hello": "world"}`

	sharedKey := []byte{194, 164, 235, 6, 138, 248, 171, 239, 24, 216, 11, 22, 137, 199, 215, 133}

	token, err := jose.Encrypt(payload, jose.DIR, jose.A128GCM, sharedKey, jose.Zip(jose.DEF))

	if err == nil {
		//go use token
		fmt.Printf("\nDIR A128GCM DEFLATED= %v\n", token)
	}
}

Verifying, Decoding and Decompressing tokens

Decoding json web tokens is fully symmetric to creating signed or encrypted tokens (with respect to public/private cryptography), decompressing deflated payloads is handled automatically:

As of v1.2 decode method defined as jose.Decode() payload string, headers map[string]interface{}, err error and returns both payload as unprocessed string and headers as map.

HS256, HS384, HS512 signatures, A128KW, A192KW, A256KW,A128GCMKW, A192GCMKW, A256GCMKW and DIR key management algorithm expecting []byte array key:

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

	token := "eyJhbGciOiJIUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.chIoYWrQMA8XL5nFz6oLDJyvgHk2KA4BrFGrKymjC8E"

	sharedKey :=[]byte{97,48,97,50,97,98,100,56,45,54,49,54,50,45,52,49,99,51,45,56,51,100,54,45,49,99,102,53,53,57,98,52,54,97,102,99}

	payload, headers, err := jose.Decode(token,sharedKey)

	if(err==nil) {
		//go use token
		fmt.Printf("\npayload = %v\n",payload)

        //and/or use headers
        fmt.Printf("\nheaders = %v\n",headers)
	}
}

RS256, RS384, RS512,PS256, PS384, PS512 signatures expecting *rsa.PublicKey public key of corresponding length. jose2go provides convenient utils to construct *rsa.PublicKey instance from PEM encoded PKCS1 X509 certificate or PKIX data: Rsa.ReadPublic([]byte) under jose2go/keys/rsa package:

…

RSA-OAEP-512, RSA-OAEP-384 ,RSA-OAEP-256, RSA-OAEP and RSA1_5 key management algorithms expecting *rsa.PrivateKey private key of corresponding length:

…

PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW key management algorithms expects string passpharase as a key

go
package main

import (
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
)

func main() {

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Goencryptionfederationjosejson

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类安全
定价开源

> 相关工具

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.