copy method
Forks the digest: a new EvpDigest with the same algorithm, buffer size, length and internal state, which evolves on its own from here on.
This is for inputs that share a prefix. Feed the prefix once, then take a
copy per variant and feed only what differs — a proof-of-work solver
hashing a fixed header with a changing nonce, a Merkle tree, a set of
messages under one envelope. The copy costs one EVP_MD_CTX_copy_ex,
about the size of the hash state, against re-feeding the prefix every
time.
The copy owns its own native context and scratch buffer: dispose it as you would the original. Neither side is affected by what the other is fed or by its release. Throws StateError once this digest is finished or disposed.
final header = openSsl.startDigest('sha256')..add(headerBytes);
for (var nonce = 0; ; nonce++) {
final attempt = header.copy();
try {
attempt.add(encodeNonce(nonce));
if (meetsTarget(attempt.finish())) break;
} finally {
attempt.dispose();
}
}
Implementation
EvpDigest copy() {
_ensureUsable();
final ctx = _bindings.EVP_MD_CTX_new();
if (ctx == nullptr) {
throw OpenSslException('Failed to create EVP_MD_CTX');
}
if (_bindings.EVP_MD_CTX_copy_ex(ctx, _ctx) != 1) {
_bindings.EVP_MD_CTX_free(ctx);
throw OpenSslException(
'EVP_MD_CTX_copy_ex failed for $algorithmName',
_bindings.ERR_get_error(),
'EVP_MD_CTX_copy_ex',
);
}
return EvpDigest._(
algorithmName,
bufferSize,
_bindings,
ctx,
_ctxFinalizer,
_length,
);
}