pull/1/head
Avril 5 years ago
parent 5186fe4b8a
commit c8ac1120ad
Signed by: flanchan
GPG Key ID: 284488987C31F630

3
.gitignore vendored

@ -2,4 +2,5 @@
Cargo.lock
*~
node_modules/
test/build/test
cli/build/test
cli/build/kana-hash

@ -13,7 +13,7 @@ crate-type = ["cdylib"]
opt-level = 3
lto = "fat"
codegen-units = 1
panic = "abort"
panic = "unwind"
[dependencies]
sha2 = "0.9"

@ -1,15 +1,20 @@
INSTALL:= /usr/local/lib
INSTALL-BIN:= /usr/local/bin
CLI:= cli
khash:
RUSTFLAGS="-C target-cpu=native" cargo build --release
strip ./target/release/libkhash.so
cd $(CLI) && $(MAKE) kana-hash
test:
cargo test
cd test && $(MAKE)
cd $(CLI) && $(MAKE)
install:
cp -f ./target/release/libkhash.so $(INSTALL)/libkhash.so
cp -f $(CLI)/build/kana-hash $(INSTALL-BIN)/kana-hash
uninstall:
rm -f $(INSTALL)/libkana_hash.so
rm -f $(INSTALL-BIN)/kana-hash

@ -1,15 +1,22 @@
SRC:= src/*.c
INCLUDE:=include/
LIB:=../target/release
BUILD:=build/
LIB:=lib
BUILD:=build
CFLAGS:= -g -Wall -pedantic
LFLAGS:= -L$(LIB) -lkhash
all: clean test
all: clean test kana-hash
clean:
rm -f $(BUILD)/*
test:
kana-hash:
gcc $(SRC) -I$(INCLUDE) $(CFLAGS) -o $(BUILD)/$@ $(LFLAGS)
$(BUILD)/$@ test
test:
gcc $(SRC) -I$(INCLUDE) -DTEST $(CFLAGS) -o $(BUILD)/$@ $(LFLAGS)
$(BUILD)/$@

@ -0,0 +1,133 @@
#include <stdio.h>
#include <assert.h>
#include <locale.h>
#include <alloca.h>
#include <string.h>
#include <khash.h>
#ifdef TEST
int main(void)
{
setlocale(LC_ALL, "");
const char* string = "hello world!";
printf("input: %s\n", string);
khash_salt salt;
assert(khash_new_salt(KHASH_SALT_TYPE_RANDOM, NULL, 0, &salt) == KHASH_SUCCESS);
printf("salt: %d\n", (int)salt.size);
size_t length;
assert(khash_length(string, strlen(string), &salt, &length) == KHASH_SUCCESS);
printf("length: %d\n", (int)length);
char* output = alloca(length+1);
assert(khash_do(string, strlen(string), &salt, output,length) == KHASH_SUCCESS);
printf("output: %s\n", output);
return 0;
}
#else
#define KTRY(expr, msg) (assert(((expr) && (msg))== KHASH_SUCCESS))
void k_do(const char* input, const khash_salt* salt)
{
khash_salt clone;
KTRY(khash_clone_salt(salt, &clone), "khash: saltclone failed");
size_t length;
KTRY(khash_length(input, strlen(input), &clone, &length), "khash: hashlength failed");
char* output =alloca(length+1);
KTRY(khash_do(input,strlen(input), &clone, output, length), "khash: hashstring failed");
output[length] = 0; //ensure no overflow.
printf("%s\n", output);
}
void reseed_salt(khash_salt* salt, uint8_t type, const void* in_ptr, size_t ptr_sz)
{
KTRY(khash_free_salt(salt), "khash: saltrefree failed");
KTRY(khash_new_salt(type, in_ptr, ptr_sz, salt), "khash: saltreseed failed");
}
static int _main(int argc, char** argv, khash_salt salt)
{
int look = 1;
if (argc <= 1)
{
printf("try `%s --help`\n", argv[0]);
return 1;
}
for(argv++;*argv;argv++)
{
if (!look) goto work;
if (strcmp(*argv, "--help") == 0)
{
printf("kana-hash cli\n");
printf("Usage: khash [--salt SALT-TYPE [<salt>]] [--] <input strings...>\n");
printf(" --salt: Specify the salt.\n");
printf(" SALT_TYPE: D: default embedded.\n");
printf(" : N: no salt.\n");
printf(" : S <salt>: specific salt.\n");
printf(" --: Stop reading args here.\n");
return 1;
}
else if (strcmp(*argv, "--") == 0)
look = 0;
else if (strcmp(*argv, "--salt")==0)
{
if (argv[1])
{
switch (argv[1][0])
{
case 'd':
case 'D':
reseed_salt(&salt, KHASH_SALT_TYPE_DEFAULT, NULL, 0);
break;
case 'N':
case 'n':
reseed_salt(&salt, KHASH_SALT_TYPE_NONE, NULL, 0);
break;
case 'S':
case 's':
if(argv[2])
reseed_salt(&salt, KHASH_SALT_TYPE_SPECIFIC, argv[2], strlen(argv[2]));
else {
fprintf(stderr, "SALT_TYPE `%c' expects a value.\n", *argv[1]);
return 1;
}
argv++;
break;
default:
fprintf(stderr, "Unknown SALT_TYPE `%c'\n", *argv[1]);
return 1;
}
argv++;
}
else {
fprintf(stderr, "--salt expects at least SALT_TYPE.\n");
return 1;
}
}
else {
work:
k_do(*argv, &salt);
}
}
return 0;
}
int main(int argc, char** argv)
{
khash_salt salt;
KTRY(khash_new_salt(KHASH_SALT_TYPE_DEFAULT, NULL, 0, &salt), "khash: saltgen failed");
int res = _main(argc, argv, salt);
KTRY(khash_free_salt(&salt), "khash: saltfree failed");
return res;
}
#endif

@ -42,6 +42,8 @@ extern "C" {
extern int32_t khash_new_salt(uint8_t salt_type, const void* data, size_t size, khash_salt* output);
/// Free a salt allocated with `khash_new_salt`. It is okay to call this multiple times.
extern int32_t khash_free_salt(khash_salt* salt);
/// Clone a salt allocated with `khash_new_salt`.
extern int32_t khash_clone_salt(const khash_salt* src, khash_salt* dst);
/// Compute the length of hash required for the specified input.
extern int32_t khash_length(const void* data, size_t size, const khash_salt* salt, size_t* length);
/// Compute the hash and store it in `string`. Will write no more than `strlen` bytes into `string`.

@ -0,0 +1,30 @@
use crate::*;
pub const ALGO_CRC32: u8 = 0;
pub const ALGO_CRC64: u8 = 1;
pub const ALGO_SHA256: u8 = 2;
#[derive(Debug,PartialEq,Eq,Hash)]
pub enum Algorithm
{
Crc32,
Crc64,
Sha256,
}
pub struct Context
{
algo: Algorithm,
salt: salt::Salt,
}
/// FFI context
#[derive(Debug)]
#[repr(C)]
pub struct CContext
{
algo: u8,
salt: *mut salt::FFI,
}

@ -161,3 +161,24 @@ pub unsafe extern "C" fn khash_new_salt(salt_type: u8, bin: *const c_void, sz: s
GENERIC_SUCCESS
}
}
#[no_mangle]
pub unsafe extern "C" fn khash_clone_salt(salt: *const salt::FFI, out: *mut salt::FFI) -> i32
{
no_unwind!{
*out = salt::into_raw(salt::clone_from_raw(salt));
GENERIC_SUCCESS
}
}
//TODO:
/*
mod ctx;
#[no_mangle]
pub unsafe extern "C" fn khash_new_context(salt: *mut salt::FFI, ctx: *mut ctx::CContext) -> i32
{
}
*/

@ -1,25 +0,0 @@
#include <stdio.h>
#include <khash.h>
#include <assert.h>
#include <locale.h>
#include <alloca.h>
#include <string.h>
int main(void)
{
setlocale(LC_ALL, "");
const char* string = "hello world!";
printf("input: %s\n", string);
khash_salt salt;
assert(khash_new_salt(KHASH_SALT_TYPE_RANDOM, NULL, 0, &salt) == KHASH_SUCCESS);
printf("salt: %d\n", (int)salt.size);
size_t length;
assert(khash_length(string, strlen(string), &salt, &length) == KHASH_SUCCESS);
printf("length: %d\n", (int)length);
char* output = alloca(length+1);
assert(khash_do(string, strlen(string), &salt, output,length) == KHASH_SUCCESS);
printf("output: %s\n", output);
return 0;
}
Loading…
Cancel
Save