Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary array copy in singlePass #408

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions csrc/hash_template.cpp.template
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ JNIEXPORT void JNICALL JNI_NAME(updateNativeByteBuffer)(
}

JNIEXPORT void JNICALL JNI_NAME(fastDigest)(
JNIEnv* pEnv, jclass, jbyteArray digestArray, jbyteArray dataArray, jint dataLength)
JNIEnv* pEnv, jclass, jbyteArray digestArray, jbyteArray dataArray, jint bufOffset, jint dataLength)
{
// As this method needs to be extremely high speed, we are omitting use of java_buffer
// to avoid the extra JNI calls it requires. Instead we are trusting that dataLength
Expand All @@ -147,14 +147,14 @@ JNIEXPORT void JNICALL JNI_NAME(fastDigest)(
}

if (static_cast<size_t>(dataLength) > scratchSize) {
java_buffer dataBuffer = java_buffer::from_array(env, dataArray, 0, dataLength);
java_buffer dataBuffer = java_buffer::from_array(env, dataArray, bufOffset, dataLength);
jni_borrow dataBorrow(env, dataBuffer, "data");
if (unlikely(!OP(Update)(ctx, dataBorrow.data(), dataBorrow.len()))) {
throw java_ex::from_openssl(EX_RUNTIME_CRYPTO, "Unable to update context");
}
} else {
SecureBuffer<uint8_t, scratchSize> scratch;
env->GetByteArrayRegion(dataArray, 0, dataLength, reinterpret_cast<jbyte*>(scratch.buf));
env->GetByteArrayRegion(dataArray, bufOffset, dataLength, reinterpret_cast<jbyte*>(scratch.buf));
if (unlikely(!OP(Update)(ctx, scratch, dataLength))) {
throw java_ex::from_openssl(EX_RUNTIME_CRYPTO, "Unable to update context");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class TemplateHashSpi extends MessageDigestSpi implements Cloneable
* @param buf Input buffer
*/
// NOTE: This method trusts that all of the array lengths and bufLen are sane.
static native void fastDigest(byte[] digest, byte[] buf, int bufLen);
static native void fastDigest(byte[] digest, byte[] buf, int bufOffset, int bufLen);
pengxiaolong marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return The size of result hashes for this hash function
Expand Down Expand Up @@ -123,12 +123,8 @@ private static byte[] doFinal(byte[] context) {
}

private static byte[] singlePass(byte[] src, int offset, int length) {
if (offset != 0 || length != src.length) {
src = Arrays.copyOf(src, length);
offset = 0;
}
final byte[] result = new byte[HASH_SIZE];
fastDigest(result, src, src.length);
fastDigest(result, src, offset, length);
return result;
}

Expand Down
Loading