feat: to_pem -> bytes_to_pem

This commit is contained in:
2022-04-04 16:05:55 +08:00
parent d90223a8ca
commit 8d988f5427
2 changed files with 25 additions and 24 deletions

View File

@@ -7,6 +7,7 @@ use base64::URL_SAFE_NO_PAD;
use rand::Rng;
use rust_util::XResult;
use serde::{Deserialize, Serialize};
use crate::pkiutil::bytes_to_pem;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct U2FDeviceInfo {
@@ -74,7 +75,7 @@ impl U2fRegistrationData {
client_data: client_data.into(),
registration_data: base64::encode(&register_result.0),
attestation_cert_pem: registration.attestation_cert.map(|c| {
to_pem(&c, "CERTIFICATE", 64)
bytes_to_pem("CERTIFICATE", c)
}),
pub_key: hex::encode(registration.pub_key),
key_handle: hex::encode(registration.key_handle),
@@ -138,29 +139,6 @@ pub fn start_status_updater() -> Sender<StatusUpdate> {
status_tx
}
pub fn to_pem(bs: &[u8], sub: &str, w: usize) -> String {
let mut s = String::with_capacity(bs.len() * 2);
s.push_str(&format!("-----BEGIN {}-----", sub));
let b64 = base64::encode(bs).chars().collect::<Vec<char>>();
let mut b64 = b64.as_slice();
while !b64.is_empty() {
s.push('\n');
if b64.len() >= w {
for c in b64.iter().take(w) {
s.push(*c);
}
b64 = &b64[w..];
} else {
for c in b64 {
s.push(*c);
}
b64 = &[];
}
}
s.push_str(&format!("\n-----END {}-----", sub));
s
}
pub fn to_hex(data: &[u8], joiner: &str) -> String {
let parts: Vec<String> = data.iter().map(|byte| format!("{:02x}", byte)).collect();
parts.join(joiner)

View File

@@ -6,6 +6,29 @@ use sequoia_openpgp::crypto::mpi::PublicKey;
use crate::digest::sha256_bytes;
// pub fn to_pem(bs: &[u8], sub: &str, w: usize) -> String {
// let mut s = String::with_capacity(bs.len() * 2);
// s.push_str(&format!("-----BEGIN {}-----", sub));
// let b64 = base64::encode(bs).chars().collect::<Vec<char>>();
// let mut b64 = b64.as_slice();
// while !b64.is_empty() {
// s.push('\n');
// if b64.len() >= w {
// for c in b64.iter().take(w) {
// s.push(*c);
// }
// b64 = &b64[w..];
// } else {
// for c in b64 {
// s.push(*c);
// }
// b64 = &[];
// }
// }
// s.push_str(&format!("\n-----END {}-----", sub));
// s
// }
pub fn bytes_to_pem<T>(tag: &str, contents: T) -> String where T: Into<Vec<u8>> {
let cert_public_key_pem_obj = Pem {
tag: tag.to_string(),