added sha256t (set to default)

pull/1/head
Avril 4 years ago
parent 22b4928bbe
commit b926ac7c39
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

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

@ -66,10 +66,11 @@ static int _main(int argc, char** argv, khash_ctx ctx)
{ {
printf("kana-hash cli\n"); printf("kana-hash cli\n");
printf("Usage: khash [--algo ALGO] [--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: Specify the algorithm. (default sha256 truncated)\n");
printf(" ALGO: 3: crc32.\n"); printf(" ALGO: 3: crc32.\n");
printf(" ALGO: 6: crc64.\n"); printf(" ALGO: 6: crc64.\n");
printf(" ALGO: s: sha256.\n"); printf(" ALGO: s: sha256.\n");
printf(" ALGO: t: truncated (64bit) 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");
@ -93,8 +94,13 @@ static int _main(int argc, char** argv, khash_ctx ctx)
ctx.algo = KHASH_ALGO_CRC64; ctx.algo = KHASH_ALGO_CRC64;
break; break;
case 's': case 's':
case 'S':
ctx.algo = KHASH_ALGO_SHA256; ctx.algo = KHASH_ALGO_SHA256;
break; break;
case 'T':
case 't':
ctx.algo = KHASH_ALGO_SHA256_TRUNCATED;
break;
default: default:
fprintf(stderr, "ALGO: unknow algorithm key `%c'\n", *argv[1]); fprintf(stderr, "ALGO: unknow algorithm key `%c'\n", *argv[1]);
return 1; return 1;

@ -17,6 +17,7 @@ extern "C" {
#define KHASH_ALGO_CRC32 ((uint8_t)1) #define KHASH_ALGO_CRC32 ((uint8_t)1)
#define KHASH_ALGO_CRC64 ((uint8_t)2) #define KHASH_ALGO_CRC64 ((uint8_t)2)
#define KHASH_ALGO_SHA256 ((uint8_t)3) #define KHASH_ALGO_SHA256 ((uint8_t)3)
#define KHASH_ALGO_SHA256_TRUNCATED ((uint8_t)4) /* SHA256 truncated to 64 bits */
/// No salt /// No salt
#define KHASH_SALT_TYPE_NONE ((uint8_t)0) #define KHASH_SALT_TYPE_NONE ((uint8_t)0)

@ -90,6 +90,7 @@ Kana.ALGO_DEFAULT = 0;
Kana.ALGO_CRC32 = 1; Kana.ALGO_CRC32 = 1;
Kana.ALGO_CRC64 = 2; Kana.ALGO_CRC64 = 2;
Kana.ALGO_SHA256 = 3; Kana.ALGO_SHA256 = 3;
Kana.ALGO_SHA256_TRUNCATED = 4;
Kana.SALT_NONE = 0; Kana.SALT_NONE = 0;
Kana.SALT_DEFAULT = 1; Kana.SALT_DEFAULT = 1;

@ -1,6 +1,6 @@
{ {
"name": "kana-hash", "name": "kana-hash",
"version": "1.0.0", "version": "1.1.0",
"description": "Kana hashes", "description": "Kana hashes",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

@ -11,13 +11,14 @@ pub enum Algorithm
Crc32, Crc32,
Crc64, Crc64,
Sha256, Sha256,
Sha256Truncated,
} }
impl Default for Algorithm impl Default for Algorithm
{ {
fn default() -> Self fn default() -> Self
{ {
Self::Crc64 Self::Sha256Truncated
} }
} }
@ -62,6 +63,7 @@ impl Context
Algorithm::Crc32 => provide::<hash::Crc32Checksum, _>(&mut from, &self.salt, &mut output)?, Algorithm::Crc32 => provide::<hash::Crc32Checksum, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Crc64 => provide::<hash::Crc64Checksum, _>(&mut from, &self.salt, &mut output)?, Algorithm::Crc64 => provide::<hash::Crc64Checksum, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Sha256 => provide::<hash::Sha256Hash, _>(&mut from, &self.salt, &mut output)?, Algorithm::Sha256 => provide::<hash::Sha256Hash, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Sha256Truncated => provide::<hash::Sha256Truncated, _>(&mut from, &self.salt, &mut output)?,
}.into_boxed_slice(); }.into_boxed_slice();
Ok((output, bytes)) Ok((output, bytes))
@ -111,6 +113,7 @@ pub const ALGO_DEFAULT: u8 = 0;
pub const ALGO_CRC32: u8 = 1; pub const ALGO_CRC32: u8 = 1;
pub const ALGO_CRC64: u8 = 2; pub const ALGO_CRC64: u8 = 2;
pub const ALGO_SHA256: u8 = 3; pub const ALGO_SHA256: u8 = 3;
pub const ALGO_SHA256_TRUNCATED: u8 = 4;
/// FFI context /// FFI context
#[derive(Debug)] #[derive(Debug)]
@ -130,6 +133,7 @@ impl From<Algorithm> for u8
Algorithm::Crc32 => ALGO_CRC32, Algorithm::Crc32 => ALGO_CRC32,
Algorithm::Crc64 => ALGO_CRC64, Algorithm::Crc64 => ALGO_CRC64,
Algorithm::Sha256 => ALGO_SHA256, Algorithm::Sha256 => ALGO_SHA256,
Algorithm::Sha256Truncated => ALGO_SHA256_TRUNCATED,
} }
} }
} }
@ -141,6 +145,7 @@ impl From<u8> for Algorithm
ALGO_CRC32 => Algorithm::Crc32, ALGO_CRC32 => Algorithm::Crc32,
ALGO_CRC64 => Algorithm::Crc64, ALGO_CRC64 => Algorithm::Crc64,
ALGO_SHA256 => Algorithm::Sha256, ALGO_SHA256 => Algorithm::Sha256,
ALGO_SHA256_TRUNCATED => Algorithm::Sha256Truncated,
_ => Self::default(), _ => Self::default(),
} }
} }

@ -8,3 +8,6 @@ pub use crc64::*;
mod crc32; mod crc32;
pub use crc32::*; pub use crc32::*;
mod sha256t;
pub use sha256t::*;

@ -0,0 +1,53 @@
use super::*;
use crate::{
array,
provider::ByteProvider,
};
pub const SHA256_TRUNCATE: usize = 8;
mod __static_assert
{
use super::*;
const _IS_LESS_THAN_OR_EQ_SHA256_SIZE: &'static [()] = &[(); SHA256_SIZE - SHA256_TRUNCATE];
}
use std::fmt;
impl fmt::Display for Sha256Truncated
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "Sha256Truncated[->{}] (", SHA256_TRUNCATE)?;
for byte in self.hash.iter()
{
write!(f, "{:02x}", *byte)?;
}
write!(f, ")")
}
}
#[repr(C)]
#[repr(packed)]
#[derive(Copy,Clone,Debug,PartialEq,Eq,Hash)]
pub struct Sha256Truncated
{
hash: [u8; SHA256_TRUNCATE],
}
impl ByteProvider for hash::Sha256Truncated
{
fn bytes(&self) -> &[u8]
{
&self.hash[..]
}
fn compute<T: Read + ?Sized>(input: &mut T, salt: &salt::Salt, done: &mut usize) -> Result<Self, error::Error>
{
let (ok, sha) = Sha256Hash::compute(input, salt)?;
let mut hash = [0u8; SHA256_TRUNCATE];
array::copy_slice(&mut hash, sha.bytes());
*done = ok;
Ok(Self{hash})
}
}

@ -15,18 +15,12 @@ mod tests {
#[test] #[test]
fn it_works() -> Result<(), error::Error> fn it_works() -> Result<(), error::Error>
{ {
let input = b"lolis are super ultra mega cute"; let input = b"lolis are super ultra mega cute!";
let context = ctx::Context::default(); let context = ctx::Context::default();
let kana = generate(&context, input)?; let kana = generate(&context, input)?;
println!("kana: {}", kana); println!("kana: {}", kana);
assert_eq!(kana, "ワイトひはぇトョ"); assert_eq!(kana, "もッちゅゆをヌョ");
Ok(())
}
#[test]
fn ffi() -> Result<(), Box<dyn std::error::Error>>
{
Ok(()) Ok(())
} }
} }
@ -36,7 +30,7 @@ pub const BUFFER_SIZE: usize = 4096;
mod array; mod array;
mod reinterpret; mod reinterpret;
mod ext; mod ext;
use ext::*; //use ext::*;
mod group; //unused mod group; //unused
mod sixteen; mod sixteen;
use sixteen::Bit16IterExt; use sixteen::Bit16IterExt;

@ -1,3 +1,5 @@
use std::ops::RangeInclusive;
pub const KANA: &[char; 92] = &[ pub const KANA: &[char; 92] = &[
'あ', 'い', 'う', 'え', 'お', 'あ', 'い', 'う', 'え', 'お',
'か', 'き', 'く', 'け', 'こ', 'か', 'き', 'く', 'け', 'こ',
@ -20,6 +22,12 @@ pub const KANA: &[char; 92] = &[
'ヤ', 'ユ', 'ヨ', 'ヤ', 'ユ', 'ヨ',
'ワ', 'ン', 'ヲ', 'ワ', 'ン', 'ヲ',
]; ];
pub const KANA_SIGN: &[RangeInclusive<usize>; 2] = &[
0..=45,
46..=91,
];
pub const KANA_SUB: &[char; 18] = &[ pub const KANA_SUB: &[char; 18] = &[
'ゃ', 'ゃ',
'ゅ', 'ゅ',

@ -16,18 +16,24 @@ impl Digest {
pub fn new(from: &[u8]) -> Self pub fn new(from: &[u8]) -> Self
{ {
let mut d = Self::default(); let mut d = Self::default();
if from.len() == 0 {
return d;
}
let oneesan = usize::from(from[0]) % map::KANA.len(); let sign0 = unsafe { *reinterpret::value::<i8>(from) < 0 };
d.0 = Some(map::KANA[oneesan]); let range = &map::KANA_SIGN[sign0 as usize];
if from[1] > 0 { let kana = &map::KANA[range.clone()];
if let Some(imoutos) = map::sub(oneesan) { let oneesan = usize::from(from[0]) % kana.len();
d.0 = Some(kana[oneesan]);
if from.len() > 1 {
if let Some(imoutos) = map::sub(range.start()+oneesan) {
if let Some(imouto) = imoutos[usize::from(from[1]) % map::KANA_SUB.len()] if let Some(imouto) = imoutos[usize::from(from[1]) % map::KANA_SUB.len()]
{ {
d.1 = Some(imouto); d.1 = Some(imouto);
return d; return d;
} }
} }
let from = [from[1], 0]; let from = [from[1]];
d.1 = Self::new(&from[..]).0; d.1 = Self::new(&from[..]).0;
} }
d d

@ -11,3 +11,10 @@ pub unsafe fn bytes_mut<'a, T>(src: &'a mut T) -> &'a mut [u8]
{ {
std::slice::from_raw_parts_mut(src as *mut T as *mut u8, std::mem::size_of_val(src)) std::slice::from_raw_parts_mut(src as *mut T as *mut u8, std::mem::size_of_val(src))
} }
pub unsafe fn value<'a, T>(src: &[u8]) -> &'a T
{
assert!(src.len() >= std::mem::size_of::<T>());
&std::slice::from_raw_parts(&src[0] as *const u8 as *const T, 1)[0]
}

@ -1,4 +1,3 @@
use crate::*;
use malloc_array::*; use malloc_array::*;
use getrandom::{ use getrandom::{
getrandom, getrandom,

Loading…
Cancel
Save