Switch back to C implementation of sha1

Now that libnettle is built with hardware acceleration instructions
This commit is contained in:
FooIbar committed 2024-06-09 23:11:57 +08:00
1 parent f50d69cd60
commit 5b15059c04
7 files changed
+40 -139

No files matched your search

+1 -1
View File
@@ -105,5 +105,5 @@ corrosion_import_crate(MANIFEST_PATH ../rust/Cargo.toml)
corrosion_add_target_local_rustflags(ehviewer_rust "-Clink-args=-Wl,-z,max-page-size=16384")
# Build and link our app's native lib
add_library(${PROJECT_NAME} SHARED archive.c border.c gifutils.c natsort/strnatcmp.c)
add_library(${PROJECT_NAME} SHARED archive.c border.c gifutils.c hash.c natsort/strnatcmp.c)
target_link_libraries(${PROJECT_NAME} ehviewer_rust archive_static log jnigraphics)
+39
View File
@@ -0,0 +1,39 @@
#include <unistd.h>
#include <jni.h>
#include <nettle/sha1.h>
#include "ehviewer.h"
#define BUFFER_SIZE 8192
typedef uint8_t byte;
const char hex_digits[] = "0123456789abcdef";
JNIEXPORT jstring JNICALL
Java_com_hippo_ehviewer_jni_HashKt_sha1(JNIEnv *env, jclass clazz, jint fd) {
EH_UNUSED(clazz);
struct sha1_ctx ctx;
sha1_init(&ctx);
size_t bytes_read;
byte buffer[BUFFER_SIZE];
while ((bytes_read = read(fd, buffer, BUFFER_SIZE)) > 0) {
sha1_update(&ctx, bytes_read, buffer);
}
byte digest[SHA1_DIGEST_SIZE];
sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
byte byte;
char hex_digest[2 * SHA1_DIGEST_SIZE + 1];
for (int i = 0; i < SHA1_DIGEST_SIZE; i++) {
byte = digest[i];
hex_digest[2 * i] = hex_digits[byte >> 4 & 0xF];
hex_digest[2 * i + 1] = hex_digits[byte & 0xF];
}
hex_digest[2 * SHA1_DIGEST_SIZE] = '\0';
return (*env)->NewStringUTF(env, hex_digest);
}
-101
View File
@@ -26,33 +26,12 @@ version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "base16ct"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bytes"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "cc"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f"
[[package]]
name = "cesu8"
version = "1.1.0"
@@ -75,42 +54,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "cpufeatures"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "ehviewer_rust"
version = "0.0.0"
dependencies = [
"android_logger",
"anyhow",
"base16ct",
"jni",
"jni_fn",
"log",
@@ -119,7 +68,6 @@ dependencies = [
"regex-lite",
"serde",
"serde_cbor",
"sha1",
"tl",
]
@@ -132,16 +80,6 @@ dependencies = [
"log",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "half"
version = "1.8.3"
@@ -181,12 +119,6 @@ dependencies = [
"syn",
]
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "log"
version = "0.4.21"
@@ -277,27 +209,6 @@ dependencies = [
"syn",
]
[[package]]
name = "sha1"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
"sha1-asm",
]
[[package]]
name = "sha1-asm"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "286acebaf8b67c1130aedffad26f594eff0c1292389158135327d2e23aed582b"
dependencies = [
"cc",
]
[[package]]
name = "syn"
version = "2.0.66"
@@ -335,24 +246,12 @@ version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b130bd8a58c163224b44e217b4239ca7b927d82bf6cc2fea1fc561d15056e3f7"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.5.0"
-2
View File
@@ -17,8 +17,6 @@ android_logger = { default-features = false, version = "0.13" }
once_cell = { default-features = false, version = "1.19" }
serde_cbor = "0.11"
serde = { version = "1.0", features = ["derive"] }
sha1 = { version = "0.10", features = ["asm"] }
base16ct = { version = "0.2", features = ["std"] }
anyhow = { version = "1.0" }
[profile.release]
-1
View File
@@ -1 +0,0 @@
mod sha1;
-31
View File
@@ -1,31 +0,0 @@
use anyhow::{Ok, Result};
use base16ct::lower::encode_str;
use jni::objects::JClass;
use jni::sys::{jint, jstring};
use jni::JNIEnv;
use jni_fn::jni_fn;
use jni_throwing;
use sha1::{Digest, Sha1};
use std::mem::forget;
use std::os::fd::FromRawFd;
use std::{fs::File, io::copy};
fn file_sha1<'a>(file: &mut File, digest: &'a mut [u8; 40]) -> Result<&'a str> {
let mut sha = Sha1::new();
copy(file, &mut sha)?;
Ok(encode_str(&sha.finalize(), digest)?)
}
#[no_mangle]
#[allow(non_snake_case)]
#[jni_fn("com.hippo.ehviewer.jni.HashKt")]
pub fn sha1(mut env: JNIEnv, _class: JClass, fd: jint) -> jstring {
jni_throwing(&mut env, |env| {
let mut hex_digest = [0u8; 40];
let mut file = unsafe { File::from_raw_fd(fd) };
let hex = file_sha1(&mut file, &mut hex_digest);
// Avoid double close, we close it on Java side
forget(file);
Ok(env.new_string(hex?)?.into_raw())
})
}
-3
View File
@@ -1,9 +1,7 @@
mod hash;
mod parser;
extern crate android_logger;
extern crate anyhow;
extern crate base16ct;
extern crate jni;
extern crate jni_fn;
extern crate log;
@@ -11,7 +9,6 @@ extern crate once_cell;
extern crate quick_xml;
extern crate regex_lite;
extern crate serde;
extern crate sha1;
extern crate tl;
use android_logger::Config;