Browse Source

Simple functions added to Ps and HashMap example

pull/47/head
Andrea Ciliberti 3 years ago
parent
commit
8ed7c97a5d
  1. 39
      ctru-rs/examples/hashmaps.rs
  2. 56
      ctru-rs/src/services/ps.rs

39
ctru-rs/examples/hashmaps.rs

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
use ctru::console::Console;
use ctru::gfx::Gfx;
use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, KeyPad};
use ctru::services::ps::Ps;
fn main() {
// Initialize services
ctru::init();
let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap();
let gfx = Gfx::default();
let _console = Console::init(gfx.top_screen.borrow_mut());
// HashMaps generate hashes thanks to the 3DS' criptografically secure generator.
// Sadly, this generator is only active when activating the `Ps` service.
// To do this, we have to make sure the `Ps` service handle is alive for the whole
// run time (or at least, when `HashMaps` are used).
// Not having a living `Ps` instance when using `HashMap`s results in a panic
let _ps = Ps::init().unwrap();
let mut map = std::collections::HashMap::new();
map.insert("A Key!", 102);
map.insert("Another key?", 543);
map.remove("A Key!");
println!("{:#?}", map);
while apt.main_loop() {
gfx.flush_buffers();
gfx.swap_buffers();
gfx.wait_for_vblank();
hid.scan_input();
if hid.keys_down().contains(KeyPad::KEY_START) {
break;
}
}
}

56
ctru-rs/src/services/ps.rs

@ -8,6 +8,30 @@ @@ -8,6 +8,30 @@
#[non_exhaustive]
pub struct Ps;
#[repr(u32)]
pub enum AESAlgorithm {
CbcEnc,
CbcDec,
CtrEnc,
CtrDec,
CcmEnc,
CcmDec,
}
#[repr(u32)]
pub enum AESKeyType {
Keyslot0D,
Keyslot2D,
Keyslot31,
Keyslot38,
Keyslot32,
Keyslot39Dlp,
Keyslot2E,
KeyslotInvalid,
Keyslot36,
Keyslot39Nfc,
}
impl Ps {
/// Initialize the PS module.
pub fn init() -> crate::Result<Self> {
@ -18,6 +42,38 @@ impl Ps { @@ -18,6 +42,38 @@ impl Ps {
Ok(Self)
}
}
pub fn local_friend_code_seed(&self) -> crate::Result<u64> {
let mut seed: u64 = 0;
let r = unsafe { ctru_sys::PS_GetLocalFriendCodeSeed(&mut seed) };
if r < 0 {
Err(r.into())
} else {
Ok(seed)
}
}
pub fn device_id(&self) -> crate::Result<u32> {
let mut id: u32 = 0;
let r = unsafe { ctru_sys::PS_GetDeviceId(&mut id) };
if r < 0 {
Err(r.into())
} else {
Ok(id)
}
}
pub fn generate_random_bytes(&self, out: &mut [u8]) -> crate::Result<()> {
let r =
unsafe { ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len() as u32) };
if r < 0 {
Err(r.into())
} else {
Ok(())
}
}
}
impl Drop for Ps {

Loading…
Cancel
Save