feat: byte_to_pem

This commit is contained in:
2022-04-04 15:59:35 +08:00
parent c9ab9820a8
commit d90223a8ca
2 changed files with 18 additions and 26 deletions

View File

@@ -6,6 +6,14 @@ use sequoia_openpgp::crypto::mpi::PublicKey;
use crate::digest::sha256_bytes;
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(),
contents: contents.into(),
};
pem::encode(&cert_public_key_pem_obj).trim().to_string()
}
pub fn sequoia_openpgp_public_key_pem(public_key: &PublicKey) -> Option<(Vec<u8>, String)> {
match public_key {
PublicKey::RSA { e, n } => {
@@ -37,9 +45,5 @@ fn internal_rsa_public_key_pem(n: &[u8], e: &[u8]) -> (Vec<u8>, String) {
);
let rsa_pub_key_bytes = rsa_pub_key.unwrap().public_key_to_der().unwrap();
let rsa_pub_key_bytes_sha256 = sha256_bytes(&rsa_pub_key_bytes);
let pub_key_pem_obj = Pem {
tag: String::from("PUBLIC KEY"),
contents: rsa_pub_key_bytes,
};
(rsa_pub_key_bytes_sha256, pem::encode(&pub_key_pem_obj))
(rsa_pub_key_bytes_sha256, bytes_to_pem("PUBLIC KEY", rsa_pub_key_bytes))
}