mirror of
https://gitlab.com/fabinfra/fabaccess/nfc_rs.git
synced 2025-03-12 06:41:46 +01:00
fixed padding function
This commit is contained in:
parent
cc836af2c4
commit
a7d0497f1f
@ -1,7 +1,7 @@
|
||||
use crate::error::{Result, Error};
|
||||
use simple_error::simple_error;
|
||||
use block_modes::block_padding::{ZeroPadding, Padding};
|
||||
|
||||
/// Extracts the the last `n` bytes of a slice. n being the blocksize.
|
||||
pub fn extract_last_block(data: &[u8], blocksize: usize) -> Result<&[u8]> {
|
||||
if data.len() % blocksize != 0 {
|
||||
return Err(simple_error!("Data is not compatible with blocksize: data(length): {}, blocksize: {}.", data.len(), blocksize).into())
|
||||
@ -10,11 +10,15 @@ pub fn extract_last_block(data: &[u8], blocksize: usize) -> Result<&[u8]> {
|
||||
Ok(&data[(data.len() - blocksize)..])
|
||||
}
|
||||
|
||||
pub fn expand_to_blocksize(data: &mut [u8], blocksize: usize) -> Result<&mut [u8]> {
|
||||
// let diff = data.len() % blocksize;
|
||||
/// Takes a given input and zero pads it to a multiple of blocksize
|
||||
pub fn expand_to_blocksize(data: &mut [u8], blocksize: usize) -> Result<Vec<u8>> {
|
||||
let diff = data.len() % blocksize;
|
||||
|
||||
match ZeroPadding::pad(data, data.len(), blocksize) {
|
||||
Ok(x) => {Ok(x)}
|
||||
Err(e) => {Err(Error::PadError(e))}
|
||||
if diff == 0 {
|
||||
return Ok(data.to_vec());
|
||||
} else {
|
||||
let mut buf = vec![0 as u8; data.len() + blocksize - diff];
|
||||
buf[..data.len()].copy_from_slice(data);
|
||||
Ok(buf)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user