jsonsign

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 17 Imported by: 0

README

JSON Signature

This is a fork of github.com/vaxvhbe/jsonsign

The jsonsign library provides functionality to digitally sign JSON data using RSA cryptography. It allows users to:

  • Sign JSON Files: It creates a secure signature for a given JSON document, ensuring the integrity and authenticity of the data. The signature is appended to the original JSON object without altering the data structure or content.
  • Validate JSON Signatures: It verifies the authenticity of the signed JSON document by checking the validity of the signature against the public key. This process ensures that the JSON data has not been tampered with and originates from a trusted source.
  • Deterministic Serialization: The library ensures that the JSON data is serialized in a consistent manner before signing, preventing issues related to key ordering or formatting that could invalidate the signature.

The library is suitable for applications that require secure communication of JSON data, ensuring both integrity and authenticity through cryptographic signatures.

Build

make

Create keys

make keys

Usage

Sign
./bin/sign -priv private.key -json myfile.json
Validate
./bin/validate -pub public.key -json myfile.json

Standards

https://cyberphone.github.io/doc/security/jsf.html

https://datatracker.ietf.org/doc/html/rfc7518

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DsaStrings = map[DSA]string{
	RS256: "RS256",
	RS384: "RS384",
	RS512: "RS512",
}

Functions

func CertificateToPem

func CertificateToPem(
	CertBytes *[]byte,
) *[]byte

func DecryptWithPrivateKey

func DecryptWithPrivateKey(
	ciphertext []byte,
	priv *rsa.PrivateKey,
) []byte

DecryptWithPrivateKey decrypts data with private key

func EncryptWithPublicKey

func EncryptWithPublicKey(
	msg []byte,
	pub *rsa.PublicKey,
) []byte

https://gist.github.com/miguelmota/3ea9286bd1d3c2a985b67cac4ba2130a EncryptWithPublicKey encrypts data with public key

func GenerateRsaCertificate

func GenerateRsaCertificate(
	Key *rsa.PrivateKey,
	CertTemplate *x509.Certificate,
) (
	Certificate *[]byte,
)

func GenerateRsaKey

func GenerateRsaKey(bitSize RsaBitSize) *rsa.PrivateKey

func GenerateSignature

func GenerateSignature(privateKey *rsa.PrivateKey, hashed []byte, alg DSA) ([]byte, error)

func JsonToHash

func JsonToHash(stableJson []byte, alg DSA) ([]byte, error)

func NewX509CertificateTemplate

func NewX509CertificateTemplate(
	Organization,
	CommonName string,
	Duration time.Duration,
) *x509.Certificate

func RsaKeyToPem

func RsaKeyToPem(Key *rsa.PrivateKey) (
	PublicKey,
	PrivateKey *[]byte,
)

func SetupAlgFlags

func SetupAlgFlags() map[DSA]*bool

func VerifySignature

func VerifySignature(publicKey *rsa.PublicKey, hashed, signature []byte, alg DSA) error

func WithPrivateKey

func WithPrivateKey(privateKey *rsa.PrivateKey) func(*JsonSign)

func WithPrivateKeyFilePath

func WithPrivateKeyFilePath(privateKeyFilePath string) func(*JsonSign)

WithPrivateKeyFilePath give the private key file path

func WithPublicKey

func WithPublicKey(publicKey *rsa.PublicKey) func(*JsonSign)

func WithPublicKeyFilePath

func WithPublicKeyFilePath(publicKeyFilePath string) func(*JsonSign)

WithPublicKeyFilePath give the public key file path

Types

type DSA

type DSA uint
const (
	RS256 DSA = 1 + iota
	RS384
	RS512
)

func ParseAlgFlag

func ParseAlgFlag(algFlags map[DSA]*bool) (*DSA, error)

func (DSA) String

func (dsa DSA) String() string

type JsonSign

type JsonSign struct {
	PrivateKey *rsa.PrivateKey
	PublicKey  *rsa.PublicKey
	Algorithm  DSA
}

JsonSign containt keys path

func New

func New(options ...func(*JsonSign)) *JsonSign

New create new instance of JsonSign

func (*JsonSign) SignBytes added in v0.1.3

func (js *JsonSign) SignBytes(jsonBytes *[]byte) (*[]byte, error)

func (*JsonSign) SignFile added in v0.1.3

func (js *JsonSign) SignFile(jsonFilePath string) error

Sign the JSON file and add a signature

func (*JsonSign) ValidateBytes added in v0.1.3

func (js *JsonSign) ValidateBytes(jsonBytes *[]byte) (bool, error)

Validate the JSON file signature

func (*JsonSign) ValidateFile added in v0.1.3

func (js *JsonSign) ValidateFile(jsonFilePath string) (bool, error)

Sign the JSON file and add a signature

type RsaBitSize

type RsaBitSize int
const (
	Rsa1024 RsaBitSize = 1024
	Rsa2048 RsaBitSize = 2048
	Rsa4096 RsaBitSize = 4096
)

Directories

Path Synopsis
cmd
sign command
validate command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL