rsa

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package rsa provides RSA-2048-SHA256 signing and verification.

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

Index

Constants

View Source
const (
	// SecretKeySize is the size of the raw secret key in bytes.
	// Format: p (128 bytes) || q (128 bytes) || d (256 bytes) || e (8 bytes)
	SecretKeySize = 520

	// PublicKeySize is the size of the raw public key in bytes.
	// Format: n (256 bytes) || e (8 bytes)
	PublicKeySize = 264

	// SignatureSize is the size of an RSA-2048 signature.
	SignatureSize = 256

	// FingerprintSize is the size of a fingerprint in bytes.
	FingerprintSize = 32
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Fingerprint

type Fingerprint [FingerprintSize]byte

Fingerprint is a 256-bit unique identifier for an RSA key.

func (*Fingerprint) MarshalText

func (f *Fingerprint) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Fingerprint) UnmarshalText

func (f *Fingerprint) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type PublicKey

type PublicKey struct {
	// contains filtered or unexported fields
}

PublicKey contains a 2048-bit RSA public key usable for verification, with SHA256 as the underlying hash algorithm.

func MustParsePublicKey

func MustParsePublicKey(b [PublicKeySize]byte) *PublicKey

MustParsePublicKey parses a 264-byte array into a public key. It panics if the parsing fails.

func MustParsePublicKeyDER

func MustParsePublicKeyDER(der []byte) *PublicKey

MustParsePublicKeyDER parses a PKIX DER-encoded public key. It panics if the parsing fails.

func MustParsePublicKeyPEM

func MustParsePublicKeyPEM(s string) *PublicKey

MustParsePublicKeyPEM parses a PEM-encoded public key. It panics if the parsing fails.

func ParsePublicKey

func ParsePublicKey(b [PublicKeySize]byte) (*PublicKey, error)

ParsePublicKey parses a 264-byte array into a public key.

Format: n (256 bytes) || e (8 bytes), all in big-endian.

func ParsePublicKeyDER

func ParsePublicKeyDER(der []byte) (*PublicKey, error)

ParsePublicKeyDER parses a PKIX DER-encoded public key.

func ParsePublicKeyPEM

func ParsePublicKeyPEM(s string) (*PublicKey, error)

ParsePublicKeyPEM parses a PEM-encoded public key.

func (*PublicKey) Fingerprint

func (k *PublicKey) Fingerprint() Fingerprint

Fingerprint returns a 256-bit unique identifier for this key. For RSA, that is the SHA256 hash of the raw (le modulus || le exponent) public key.

func (*PublicKey) Marshal

func (k *PublicKey) Marshal() [PublicKeySize]byte

Marshal serializes the public key into a 264-byte array.

Format: n (256 bytes) || e (8 bytes), all in big-endian.

func (*PublicKey) MarshalCBOR

func (k *PublicKey) MarshalCBOR(enc *cbor.Encoder) error

MarshalCBOR implements cbor.Marshaler.

func (*PublicKey) MarshalDER

func (k *PublicKey) MarshalDER() []byte

MarshalDER serializes the public key to PKIX DER format.

func (*PublicKey) MarshalPEM

func (k *PublicKey) MarshalPEM() string

MarshalPEM serializes the public key to PEM format.

func (*PublicKey) MarshalText

func (k *PublicKey) MarshalText() ([]byte, error)

func (*PublicKey) UnmarshalCBOR

func (k *PublicKey) UnmarshalCBOR(dec *cbor.Decoder) error

UnmarshalCBOR implements cbor.Unmarshaler.

func (*PublicKey) UnmarshalText

func (k *PublicKey) UnmarshalText(text []byte) error

func (*PublicKey) Verify

func (k *PublicKey) Verify(message []byte, sig *Signature) error

Verify verifies a digital signature.

func (*PublicKey) VerifyHash

func (k *PublicKey) VerifyHash(hash []byte, sig *Signature) error

VerifyHash verifies a digital signature on an already hashed message.

type SecretKey

type SecretKey struct {
	// contains filtered or unexported fields
}

SecretKey contains a 2048-bit RSA private key usable for signing, with SHA256 as the underlying hash algorithm.

func GenerateKey

func GenerateKey() *SecretKey

GenerateKey creates a new, random private key.

func MustParseSecretKey

func MustParseSecretKey(b [SecretKeySize]byte) *SecretKey

MustParseSecretKey parses a 520-byte array into a private key. It panics if the parsing fails.

func MustParseSecretKeyDER

func MustParseSecretKeyDER(der []byte) *SecretKey

MustParseSecretKeyDER parses a PKCS#8 DER-encoded private key. It panics if the parsing fails.

func MustParseSecretKeyPEM

func MustParseSecretKeyPEM(s string) *SecretKey

MustParseSecretKeyPEM parses a PEM-encoded private key. It panics if the parsing fails.

func ParseSecretKey

func ParseSecretKey(b [SecretKeySize]byte) (*SecretKey, error)

ParseSecretKey parses a 520-byte array into a private key.

Format: p (128 bytes) || q (128 bytes) || d (256 bytes) || e (8 bytes), all in big-endian.

func ParseSecretKeyDER

func ParseSecretKeyDER(der []byte) (*SecretKey, error)

ParseSecretKeyDER parses a PKCS#8 DER-encoded private key.

func ParseSecretKeyPEM

func ParseSecretKeyPEM(s string) (*SecretKey, error)

ParseSecretKeyPEM parses a PEM-encoded private key.

func (*SecretKey) Fingerprint

func (k *SecretKey) Fingerprint() Fingerprint

Fingerprint returns a 256-bit unique identifier for this key. For RSA, that is the SHA256 hash of the raw (le modulus || le exponent) public key.

func (*SecretKey) Marshal

func (k *SecretKey) Marshal() [SecretKeySize]byte

Marshal serializes the private key into a 520-byte array.

Format: p (128 bytes) || q (128 bytes) || d (256 bytes) || e (8 bytes), all in big-endian.

func (*SecretKey) MarshalDER

func (k *SecretKey) MarshalDER() []byte

MarshalDER serializes the private key to PKCS#8 DER format.

func (*SecretKey) MarshalPEM

func (k *SecretKey) MarshalPEM() string

MarshalPEM serializes the private key to PEM format.

func (*SecretKey) PublicKey

func (k *SecretKey) PublicKey() *PublicKey

PublicKey returns the public counterpart of the secret key.

func (*SecretKey) Sign

func (k *SecretKey) Sign(message []byte) (*Signature, error)

Sign creates a digital signature of the message using PKCS#1 v1.5. This call will never return an error, the type is there for composability.

type Signature

type Signature [SignatureSize]byte

Signature contains an RSA-2048 signature.

func (*Signature) MarshalText

func (s *Signature) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Signature) UnmarshalText

func (s *Signature) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

Jump to

Keyboard shortcuts

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