use hmac::{Hmac, Mac};
use sha2::Sha256;
use wasm_bindgen::prelude::*;
type HmacSha256 = Hmac<Sha256>;
mod v0;
mod v1;
mod v2;
mod v3;
#[wasm_bindgen]
#[derive(PartialEq)]
pub enum Platform {
Wii,
Dsi,
The3ds,
WiiU,
Switch,
}
pub use v0::calculate_v0_master_key;
pub use v1::{V1Error, calculate_v1_master_key};
pub use v2::{V2Error, calculate_v2_master_key};
pub use v3::{V3Error, calculate_v3_master_key};
fn calculate_master_key_shared_v1_and_v2(
hmac_key: &[u8; 32],
inquiry_number: u64,
day: u8,
month: u8,
big_endian: bool,
) -> u32 {
let input = format!("{month:0>2}{day:0>2}{inquiry_number:0>10}");
#[allow(clippy::expect_used)]
let mut hmac = HmacSha256::new_from_slice(hmac_key).expect("Invalid lenght of the key");
hmac.update(input.as_bytes());
#[allow(clippy::expect_used)]
let hash: [u8; 4] = hmac.finalize().into_bytes()[0..4]
.try_into()
.expect("The HMAC hash is always long enough");
let output = if big_endian {
u32::from_be_bytes(hash)
} else {
u32::from_le_bytes(hash)
};
output % 100000
}