Class RsaSigDataContentBuilder
java.lang.Object
zeroecho.sdk.builders.alg.RsaSigDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<PlainContent>
public final class RsaSigDataContentBuilder
extends Object
implements DataContentBuilder<PlainContent>
RsaSigDataContentBuilder builds streaming RSA signature pipelines that sign
or verify as an InputStream is consumed.
Overview
The builder producesPlainContent that either passes the original
bytes through while appending or checking a detached signature trailer, or
emits only the signature or a boolean verification result. The signature
variant (PKCS#1 v1.5 or PSS) is configured via spec(RsaSigSpec),
pkcs1v15(RsaSigSpec.Hash), or pss(RsaSigSpec.Hash, int).
Keys may be supplied, generated, or imported from standard encodings.
Usage examples
// Sign while passing the body through; capture signature via callback:
PlainContent sign = RsaSigDataContentBuilder.builder()
.pss(RsaSigSpec.Hash.SHA256, 32)
.withPrivateKey(priv)
.onSignature(sig -> System.out.println(sig.length))
.passThrough()
.sign()
.build(true);
// Emit a Base64 detached signature:
PlainContent sigOnly = RsaSigDataContentBuilder.builder()
.pkcs1v15(RsaSigSpec.Hash.SHA512)
.withPrivateKey(priv)
.emitBase64Signature()
.build(true);
// Verify against an expected signature and emit "true"/"false":
PlainContent verdict = RsaSigDataContentBuilder.builder()
.withPublicKey(pub)
.pss(RsaSigSpec.Hash.SHA256, 32)
.expectedSignatureBase64(b64)
.emitVerificationBoolean()
.build(true);
Thread-safety
Instances are mutable and not thread-safe. Configure and use each builder instance from a single thread.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumMode selects whether the builder signs or verifies. -
Method Summary
Modifier and TypeMethodDescriptionbuild(boolean ignored) Builds a streaming signing or verification pipeline according to the current configuration.static RsaSigDataContentBuilderbuilder(ZeroEchoSession session) Creates a new builder for constructing RSA signing or verification pipelines.Configures sign mode to emit the finalized signature as Base64 text.Configures sign mode to emit the finalized signature as lowercase hexadecimal text.Configures sign mode to emit the finalized signature as raw bytes.Configures verify mode to emit a textual boolean result.expectedSignature(byte[] raw) Supplies the expected signature as raw bytes for verification.Supplies the expected signature as a Base64 string for verification.Supplies the expected signature as a hexadecimal string for verification.generateKeyPair(RsaKeyGenSpec spec) Requests generation of a fresh RSA key pair with the provided key generation specification.importPrivatePkcs8(byte[] pkcs8) Imports a private key from PKCS#8-encoded bytes.importPublicX509(byte[] x509) Imports a public key from X.509 SubjectPublicKeyInfo bytes.onSignature(Consumer<byte[]> cb) Registers a callback that receives a defensive copy of the finalized signature bytes in sign mode.onVerified(Consumer<Boolean> cb) Registers a callback that receives the verification result in verify mode.Configures the pipeline to pass the input body through unchanged while appending or checking a trailer.Selects the PKCS#1 v1.5 signature scheme with the given hash function.pss(RsaSigSpec.Hash h, int saltLen) Selects the RSASSA-PSS signature scheme with the given hash function and salt length.sign()Switches the builder to sign mode.spec(RsaSigSpec s) Sets the RSA signature specification, including padding mode and hash options.verify()Switches the builder to verify mode.Supplies the private key to use for signing.Supplies the public key to use for verification.
-
Method Details
-
builder
Creates a new builder for constructing RSA signing or verification pipelines.RsaSigDataContentBuilder b = RsaSigDataContentBuilder.builder();- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new
RsaSigDataContentBuilderinstance
-
sign
Switches the builder to sign mode.- Returns:
thisbuilder for chaining
-
verify
Switches the builder to verify mode.- Returns:
thisbuilder for chaining
-
passThrough
Configures the pipeline to pass the input body through unchanged while appending or checking a trailer.- Returns:
thisbuilder for chaining
-
emitRawSignature
Configures sign mode to emit the finalized signature as raw bytes.- Returns:
thisbuilder for chaining
-
emitHexSignature
Configures sign mode to emit the finalized signature as lowercase hexadecimal text.- Returns:
thisbuilder for chaining
-
emitBase64Signature
Configures sign mode to emit the finalized signature as Base64 text.- Returns:
thisbuilder for chaining
-
emitVerificationBoolean
Configures verify mode to emit a textual boolean result.- Returns:
thisbuilder for chaining
-
spec
Sets the RSA signature specification, including padding mode and hash options.- Parameters:
s- the RSA signature spec; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifsis null
-
pkcs1v15
Selects the PKCS#1 v1.5 signature scheme with the given hash function.- Parameters:
h- the hash function to use- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifhis null
-
pss
Selects the RSASSA-PSS signature scheme with the given hash function and salt length.- Parameters:
h- the hash function to use for both message hash and MGF1saltLen- the salt length in bytes- Returns:
thisbuilder for chaining
-
withPrivateKey
Supplies the private key to use for signing.- Parameters:
k- the private key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifkis null
-
withPublicKey
Supplies the public key to use for verification.- Parameters:
k- the public key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifkis null
-
generateKeyPair
Requests generation of a fresh RSA key pair with the provided key generation specification.- Parameters:
spec- the RSA key generation spec; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifspecis null
-
importPrivatePkcs8
Imports a private key from PKCS#8-encoded bytes.- Parameters:
pkcs8- PKCS#8 PrivateKeyInfo bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifpkcs8is null
-
importPublicX509
Imports a public key from X.509 SubjectPublicKeyInfo bytes.- Parameters:
x509- X.509 public key bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifx509is null
-
expectedSignature
Supplies the expected signature as raw bytes for verification.- Parameters:
raw- the expected signature bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifrawis null
-
expectedSignatureHex
Supplies the expected signature as a hexadecimal string for verification.- Parameters:
hex- the expected signature in hexadecimal; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifhexis nullIllegalArgumentException- ifhexis not valid hexadecimal
-
expectedSignatureBase64
Supplies the expected signature as a Base64 string for verification.- Parameters:
b64- the expected signature in Base64; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifb64is nullIllegalArgumentException- ifb64is not valid Base64
-
onSignature
Registers a callback that receives a defensive copy of the finalized signature bytes in sign mode.- Parameters:
cb- the callback to invoke; may be null to clear- Returns:
thisbuilder for chaining
-
onVerified
Registers a callback that receives the verification result in verify mode.- Parameters:
cb- the callback to invoke withtrueon success andfalseon failure; may be null to clear- Returns:
thisbuilder for chaining
-
build
Builds a streaming signing or verification pipeline according to the current configuration.The boolean parameter is ignored and exists only to satisfy the
DataContentBuilderinterface; behavior is determined bysign()orverify()and the selected output.Result
- Sign mode: returns pass-through content that strips the trailer and
optionally invokes
onSignature(Consumer)or an emitter of the signature in raw, hex, or Base64. - Verify mode: returns pass-through content that throws on mismatch and can
invoke
onVerified(Consumer), or an emitter of "true"/"false".
- Specified by:
buildin interfaceDataContentBuilder<PlainContent>- Parameters:
ignored- ignored parameter from the interface- Returns:
- a
PlainContentpipeline performing the configured RSA signature operation - Throws:
IllegalStateException- if required key material is missing or key setup fails
- Sign mode: returns pass-through content that strips the trailer and
optionally invokes
-