use ssb_crypto::secretbox::Nonce;
pub struct NonceGen {
next_nonce: Nonce,
}
impl NonceGen {
pub fn with_starting_nonce(nonce: Nonce) -> NonceGen {
NonceGen { next_nonce: nonce }
}
pub fn next(&mut self) -> Nonce {
let n = self.next_nonce;
for byte in self.next_nonce.0.iter_mut().rev() {
*byte = byte.wrapping_add(1);
if *byte != 0 {
break;
}
}
n
}
}
#[test]
fn increment() {
use crate::NonceGen;
use ssb_crypto::secretbox::Nonce;
let nonce_bytes = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255,
];
let mut gen = NonceGen::with_starting_nonce(Nonce(nonce_bytes));
let n1 = gen.next();
assert_eq!(&n1.0, &nonce_bytes);
let n2 = gen.next();
assert_eq!(
&n2.0,
&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
);
}