EvpDigest constructor
EvpDigest(
- OpenSslContext context,
- String algorithmName, {
- int bufferSize = defaultBufferSize,
Starts a digest with algorithmName, as named by OpenSSL — sha256,
sha512, sha3-256, sha1, … (the DigestAlgorithm constants are
these names, spell-checked by the compiler).
bufferSize is the size of the scratch buffer used to hand chunks to
OpenSSL, and therefore the ceiling on native memory held by this object.
The default suits file and socket streams; there is no gain in making it
larger than the chunks actually being fed.
Throws OpenSslException if the loaded OpenSSL does not know
algorithmName — including when it lives in a provider that has not been
loaded, such as the legacy provider for older algorithms.
Implementation
factory EvpDigest(
OpenSslContext context,
String algorithmName, {
int bufferSize = defaultBufferSize,
}) {
if (bufferSize <= 0) {
throw ArgumentError.value(bufferSize, 'bufferSize', 'must be positive');
}
final bindings = context.bindings;
final arena = Arena();
final Pointer<EVP_MD> md;
try {
final namePtr = algorithmName.toNativeUtf8(allocator: arena);
md = bindings.EVP_get_digestbyname(namePtr.cast());
} finally {
arena.releaseAll();
}
if (md == nullptr) {
throw OpenSslException('Unknown digest algorithm: $algorithmName');
}
final ctx = bindings.EVP_MD_CTX_new();
if (ctx == nullptr) {
throw OpenSslException('Failed to create EVP_MD_CTX');
}
if (bindings.EVP_DigestInit_ex(ctx, md, nullptr) != 1) {
bindings.EVP_MD_CTX_free(ctx);
throw OpenSslException('EVP_DigestInit_ex failed for $algorithmName');
}
// Safety net for a digest abandoned without [finish] or [dispose]: the
// context is freed when the object is collected. Shared with every copy
// taken from this digest, since they free their contexts the same way.
final ctxFinalizer = NativeFinalizer(
context
.lookup<Void Function(Pointer<EVP_MD_CTX>)>('EVP_MD_CTX_free')
.cast(),
);
return EvpDigest._(
algorithmName, bufferSize, bindings, ctx, ctxFinalizer, 0);
}