From e6edae7e992bb276fea9538bab9cdad867f5f423 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 24 Jun 2020 14:54:17 +0100 Subject: [PATCH] added swap2 --- Cargo.toml | 2 +- src/lib.rs | 11 +++++++++++ src/map.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++-- src/mnemonic.rs | 34 ++++++--------------------------- 4 files changed, 67 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a7806ca..a189ace 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "khash" description = "Kana hashes" -version = "1.1.0" +version = "1.2.0" authors = ["Avril "] edition = "2018" license = "GPL-3.0-or-later" diff --git a/src/lib.rs b/src/lib.rs index 1328d85..0b47ced 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,17 @@ mod tests { assert_eq!(kana, "もッちゅゆをヌョ"); Ok(()) } + #[test] + fn rng() + { + let input = b"loli"; + for _ in 0..100 + { + let context = ctx::Context::new(ctx::Algorithm::Sha256, salt::Salt::random().unwrap()); + let kana = generate(&context, input).unwrap(); + println!("kana: {}", kana); + } + } } pub const BUFFER_SIZE: usize = 4096; diff --git a/src/map.rs b/src/map.rs index c3c1f65..393cfed 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,5 +1,6 @@ use std::ops::RangeInclusive; +/// All valid kana for the digest pub const KANA: &[char; 92] = &[ 'あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', @@ -19,10 +20,56 @@ pub const KANA: &[char; 92] = &[ 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', 'マ', 'ミ', 'ム', 'メ', 'モ', 'ラ', 'リ', 'ル', 'レ', 'ロ', - 'ヤ', 'ユ', 'ヨ', - 'ワ', 'ン', 'ヲ', + 'ヤ', 'ユ', 'ヨ', 'ワ', 'ン', + 'ヲ', //91 ]; +/// Valid kana for the +pub const KANA_SWAP: &[Option; 92] = &[ + None, None, None, None, None, //a + Some('が'), Some('ぎ'), Some('ぐ'), Some('げ'), Some('ご'), //ka + None, None, None, None, None, //sa + Some('だ'), Some('ぢ'), Some('づ'), Some('で'), Some('ど'), //ta + None, None, None, None, None, //na + Some('ば'), Some('び'), Some('ぶ'), Some('べ'), Some('ぼ'), // ha + None, None, None, None, None, //ma + None, None, None, None, None, //ra + None, None, None, None, None, //ya + None, //wo (45) + None, None, None, None, None, //a + Some('ガ'), Some('ギ'), Some('グ'), Some('ゲ'), Some('ゴ'), //ka + None, None, None, None, None, //sa + Some('ダ'), Some('ヂ'), Some('ヅ'), Some('デ'), Some('ド'), //ta + None, None, None, None, None, //na + Some('バ'), Some('ビ'), Some('ブ'), Some('ベ'), Some('ボ'), // ha + None, None, None, None, None, //ma + None, None, None, None, None, //ra + None, None, None, None, None, //ya + None, //wo (91) +]; + +pub const KANA_SWAP2: &[Option; 92] = &[ + None, None, None, None, None, //a + None, None, None, None, None, //ka + None, None, None, None, None, //sa + None, None, None, None, None, //ta + None, None, None, None, None, //na + Some('ぱ'), Some('ぴ'), Some('ぽ'), Some('ぺ'), Some('ぽ'), //ha + None, None, None, None, None, //ma + None, None, None, None, None, //ra + None, None, None, None, None, //ya + None, //wo (45) + None, None, None, None, None, //a + None, None, None, None, None, //ka + None, None, None, None, None, //sa + None, None, None, None, None, //ta + None, None, None, None, None, //na + Some('パ'), Some('ピ'), Some('プ'), Some('ペ'), Some('ポ'), //ha + None, None, None, None, None, //ma + None, None, None, None, None, //ra + None, None, None, None, None, //ya + None, //wo (91) +]; pub const KANA_SIGN: &[RangeInclusive; 2] = &[ 0..=45, 46..=91, diff --git a/src/mnemonic.rs b/src/mnemonic.rs index 4d6aba5..9b53ae8 100644 --- a/src/mnemonic.rs +++ b/src/mnemonic.rs @@ -24,7 +24,12 @@ impl Digest { let range = &map::KANA_SIGN[sign0 as usize]; let kana = &map::KANA[range.clone()]; let oneesan = usize::from(from[0]) % kana.len(); - d.0 = Some(kana[oneesan]); + + d.0 = Some(match map::KANA_SWAP[oneesan] { + Some(swap) if (from[0] & 0x2) == 0x2 => swap, + Some(_) if (from[0] & 0x8) == 0x8 && map::KANA_SWAP2[oneesan].is_some() => map::KANA_SWAP2[oneesan].unwrap(), + _ => 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()] @@ -37,33 +42,6 @@ impl Digest { d.1 = Self::new(&from[..]).0; } d - // Old - /*let mut d = Self::default(); - - let oneesan = usize::from(from[0]) % map::KANA.len(); - d.0 = Some(map::KANA[oneesan]); - if from[1] > 0 { - if let Some(imoutos) = map::sub(oneesan) { - let one = (usize::from(from[1]) / map::KANA.len()) % 2; - if imoutos.len() > 0 && one > 0{ - d.1 = Some(imoutos[usize::from(from[1]) % imoutos.len()]); - return d; - } - } - let from = [from[1], 0]; - d.1 = Self::new(&from[..]).0; - } - d*/ - /*let oneesan = usize::from(from) % map::KANA.len(); - d.0 = Some(map::KANA[oneesan]); - if let Some(imoutos) = map::sub(oneesan) { - if imoutos.len() > 0 { - } - } else { - - } - - return d;*/ } }