use super::{Command, Response};
use session::{Session, SessionError};
use sha2::{Digest, Sha256};
use Adapter;
use {CommandType, ObjectId};
pub fn sign_rsa_pkcs1v15_sha256<A: Adapter>(
session: &mut Session<A>,
key_id: ObjectId,
data: &[u8],
) -> Result<RSAPKCS1Signature, SessionError> {
session.send_command(SignDataPKCS1Command {
key_id,
digest: Sha256::digest(data).as_slice().into(),
})
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct SignDataPKCS1Command {
pub key_id: ObjectId,
pub digest: Vec<u8>,
}
impl Command for SignDataPKCS1Command {
type ResponseType = RSAPKCS1Signature;
}
#[derive(Serialize, Deserialize, Debug)]
pub struct RSAPKCS1Signature(pub Vec<u8>);
impl Response for RSAPKCS1Signature {
const COMMAND_TYPE: CommandType = CommandType::SignDataPKCS1;
}
#[allow(
unknown_lints,
renamed_and_removed_lints,
len_without_is_empty
)]
impl RSAPKCS1Signature {
pub fn into_vec(self) -> Vec<u8> {
self.into()
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
self.as_ref()
}
}
impl AsRef<[u8]> for RSAPKCS1Signature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl Into<Vec<u8>> for RSAPKCS1Signature {
fn into(self) -> Vec<u8> {
self.0
}
}