|
|
@ -10,18 +10,19 @@
|
|
|
|
int main(void)
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
|
|
|
|
const char* string = "hello world!";
|
|
|
|
const char* string = "hello world!";
|
|
|
|
|
|
|
|
|
|
|
|
printf("input: %s\n", string);
|
|
|
|
printf("input: %s\n", string);
|
|
|
|
khash_salt salt;
|
|
|
|
khash_ctx ctx;
|
|
|
|
assert(khash_new_salt(KHASH_SALT_TYPE_RANDOM, NULL, 0, &salt) == KHASH_SUCCESS);
|
|
|
|
assert(khash_new_context(KHASH_ALGO_SHA256, KHASH_SALT_TYPE_NONE, NULL, 0, &ctx) == KHASH_SUCCESS);
|
|
|
|
printf("salt: %d\n", (int)salt.size);
|
|
|
|
printf("salt: %d\n", (int)ctx.salt.size);
|
|
|
|
size_t length;
|
|
|
|
size_t length;
|
|
|
|
assert(khash_length(string, strlen(string), &salt, &length) == KHASH_SUCCESS);
|
|
|
|
assert(khash_length(&ctx, string, strlen(string), &length) == KHASH_SUCCESS);
|
|
|
|
printf("length: %d\n", (int)length);
|
|
|
|
printf("length: %d\n", (int)length);
|
|
|
|
char* output = alloca(length+1);
|
|
|
|
char* output = alloca(length+1);
|
|
|
|
assert(khash_do(string, strlen(string), &salt, output,length) == KHASH_SUCCESS);
|
|
|
|
assert(khash_do(&ctx, string, strlen(string), output,length) == KHASH_SUCCESS);
|
|
|
|
|
|
|
|
output[length] = 0;
|
|
|
|
printf("output: %s\n", output);
|
|
|
|
printf("output: %s\n", output);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -29,27 +30,27 @@ int main(void)
|
|
|
|
|
|
|
|
|
|
|
|
#define KTRY(expr, msg) (assert(((expr) && (msg))== KHASH_SUCCESS))
|
|
|
|
#define KTRY(expr, msg) (assert(((expr) && (msg))== KHASH_SUCCESS))
|
|
|
|
|
|
|
|
|
|
|
|
void k_do(const char* input, const khash_salt* salt)
|
|
|
|
void k_do(const khash_ctx* ctx, const char* input)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
khash_salt clone;
|
|
|
|
khash_ctx clone;
|
|
|
|
KTRY(khash_clone_salt(salt, &clone), "khash: saltclone failed");
|
|
|
|
KTRY(khash_clone_context(ctx, &clone), "khash: ctxclone failed");
|
|
|
|
|
|
|
|
|
|
|
|
size_t length;
|
|
|
|
size_t length;
|
|
|
|
KTRY(khash_length(input, strlen(input), &clone, &length), "khash: hashlength failed");
|
|
|
|
KTRY(khash_length(&clone, input, strlen(input), &length), "khash: hashlength failed");
|
|
|
|
|
|
|
|
|
|
|
|
char* output =alloca(length+1);
|
|
|
|
char* output =alloca(length+1);
|
|
|
|
KTRY(khash_do(input,strlen(input), &clone, output, length), "khash: hashstring failed");
|
|
|
|
KTRY(khash_do(&clone, input,strlen(input), output, length), "khash: hashstring failed");
|
|
|
|
output[length] = 0; //ensure no overflow.
|
|
|
|
output[length] = 0; //ensure no overflow.
|
|
|
|
printf("%s\n", output);
|
|
|
|
printf("%s\n", output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reseed_salt(khash_salt* salt, uint8_t type, const void* in_ptr, size_t ptr_sz)
|
|
|
|
void reseed_ctx(khash_ctx* ctx, uint8_t algo, uint8_t type, const void* in_ptr, size_t ptr_sz)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
KTRY(khash_free_salt(salt), "khash: saltrefree failed");
|
|
|
|
KTRY(khash_free_context(ctx), "khash: ctxrefree failed");
|
|
|
|
KTRY(khash_new_salt(type, in_ptr, ptr_sz, salt), "khash: saltreseed failed");
|
|
|
|
KTRY(khash_new_context(algo, type, in_ptr, ptr_sz, ctx), "khash: ctxreseed failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int _main(int argc, char** argv, khash_salt salt)
|
|
|
|
static int _main(int argc, char** argv, khash_ctx ctx)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int look = 1;
|
|
|
|
int look = 1;
|
|
|
|
if (argc <= 1)
|
|
|
|
if (argc <= 1)
|
|
|
@ -64,16 +65,43 @@ static int _main(int argc, char** argv, khash_salt salt)
|
|
|
|
if (strcmp(*argv, "--help") == 0)
|
|
|
|
if (strcmp(*argv, "--help") == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("kana-hash cli\n");
|
|
|
|
printf("kana-hash cli\n");
|
|
|
|
printf("Usage: khash [--salt SALT-TYPE [<salt>]] [--] <input strings...>\n");
|
|
|
|
printf("Usage: khash [--algo ALGO] [--salt SALT-TYPE [<salt>]] [--] <input strings...>\n");
|
|
|
|
|
|
|
|
printf(" --algo: Specify the algorithm. (default crc64)\n");
|
|
|
|
|
|
|
|
printf(" ALGO: 3: crc32.\n");
|
|
|
|
|
|
|
|
printf(" ALGO: 6: crc64.\n");
|
|
|
|
|
|
|
|
printf(" ALGO: s: sha256.\n");
|
|
|
|
printf(" --salt: Specify the salt.\n");
|
|
|
|
printf(" --salt: Specify the salt.\n");
|
|
|
|
printf(" SALT_TYPE: D: default embedded.\n");
|
|
|
|
printf(" SALT_TYPE: D: default embedded.\n");
|
|
|
|
printf(" : N: no salt.\n");
|
|
|
|
printf(" : N: no salt.\n");
|
|
|
|
|
|
|
|
printf(" : R: random salt.\n");
|
|
|
|
printf(" : S <salt>: specific salt.\n");
|
|
|
|
printf(" : S <salt>: specific salt.\n");
|
|
|
|
printf(" --: Stop reading args here.\n");
|
|
|
|
printf(" --: Stop reading args here.\n");
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(*argv, "--") == 0)
|
|
|
|
else if (strcmp(*argv, "--") == 0)
|
|
|
|
look = 0;
|
|
|
|
look = 0;
|
|
|
|
|
|
|
|
else if (strcmp(*argv, "--algo")==0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (argv[1])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
switch(argv[1][0])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case '3':
|
|
|
|
|
|
|
|
ctx.algo = KHASH_ALGO_CRC32;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '6':
|
|
|
|
|
|
|
|
ctx.algo = KHASH_ALGO_CRC64;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
|
|
|
|
ctx.algo = KHASH_ALGO_SHA256;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
fprintf(stderr, "ALGO: unknow algorithm key `%c'\n", *argv[1]);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (strcmp(*argv, "--salt")==0)
|
|
|
|
else if (strcmp(*argv, "--salt")==0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (argv[1])
|
|
|
|
if (argv[1])
|
|
|
@ -82,22 +110,26 @@ static int _main(int argc, char** argv, khash_salt salt)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
case 'D':
|
|
|
|
reseed_salt(&salt, KHASH_SALT_TYPE_DEFAULT, NULL, 0);
|
|
|
|
reseed_ctx(&ctx, ctx.algo, KHASH_SALT_TYPE_DEFAULT, NULL, 0);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
case 'N':
|
|
|
|
case 'n':
|
|
|
|
case 'n':
|
|
|
|
reseed_salt(&salt, KHASH_SALT_TYPE_NONE, NULL, 0);
|
|
|
|
reseed_ctx(&ctx, ctx.algo, KHASH_SALT_TYPE_NONE, NULL, 0);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
case 'S':
|
|
|
|
case 's':
|
|
|
|
case 's':
|
|
|
|
if(argv[2])
|
|
|
|
if(argv[2])
|
|
|
|
reseed_salt(&salt, KHASH_SALT_TYPE_SPECIFIC, argv[2], strlen(argv[2]));
|
|
|
|
reseed_ctx(&ctx, ctx.algo, KHASH_SALT_TYPE_SPECIFIC, argv[2], strlen(argv[2]));
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "SALT_TYPE `%c' expects a value.\n", *argv[1]);
|
|
|
|
fprintf(stderr, "SALT_TYPE `%c' expects a value.\n", *argv[1]);
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv++;
|
|
|
|
argv++;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'R':
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
|
|
|
|
reseed_ctx(&ctx, ctx.algo, KHASH_SALT_TYPE_RANDOM, NULL, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown SALT_TYPE `%c'\n", *argv[1]);
|
|
|
|
fprintf(stderr, "Unknown SALT_TYPE `%c'\n", *argv[1]);
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
@ -111,7 +143,7 @@ static int _main(int argc, char** argv, khash_salt salt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
work:
|
|
|
|
work:
|
|
|
|
k_do(*argv, &salt);
|
|
|
|
k_do(&ctx, *argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -120,13 +152,14 @@ static int _main(int argc, char** argv, khash_salt salt)
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
|
|
|
|
khash_salt salt;
|
|
|
|
khash_ctx context;
|
|
|
|
KTRY(khash_new_salt(KHASH_SALT_TYPE_DEFAULT, NULL, 0, &salt), "khash: saltgen failed");
|
|
|
|
KTRY(khash_new_context(KHASH_ALGO_DEFAULT, KHASH_SALT_TYPE_DEFAULT, NULL, 0, &context), "khash: ctxgen failed");
|
|
|
|
|
|
|
|
|
|
|
|
int res = _main(argc, argv, salt);
|
|
|
|
int res = _main(argc, argv, context);
|
|
|
|
|
|
|
|
|
|
|
|
KTRY(khash_free_salt(&salt), "khash: saltfree failed");
|
|
|
|
KTRY(khash_free_context(&context), "khash: ctxfree failed");
|
|
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|