sequoia-cryptoki 0.1.1

Library for working with PKCS#11 devices (cryptokis) from Sequoia
Documentation
//! Object Identifiers.

#![allow(non_upper_case_globals)]

use std::borrow::Cow;

/// The DER encoding of X25519's OID.
///
/// X25519's OID is 1.3.101.110 and is specified in [section 3 of RFC
/// 8410].
///
/// [section 3 of RFC 8410]: https://www.rfc-editor.org/rfc/rfc8410.html#section-3
pub const X25519: &[u8] = &[0x06, 0x03, 0x2B, 0x65, 0x6E];

/// The DER encoding of X448's OID.
///
/// X448's OID is 1.3.101.111 and is specified in [section 3 of RFC
/// 8410].
///
/// [section 3 of RFC 8410]: https://www.rfc-editor.org/rfc/rfc8410.html#section-3
pub const X448: &[u8] = &[0x06, 0x03, 0x2B, 0x65, 0x6F];

/// The DER encoding of Ed25519's OID.
///
/// Ed25519's OID is 1.3.101.112 and is specified in [section 3 of RFC
/// 8410].
///
/// [section 3 of RFC 8410]: https://www.rfc-editor.org/rfc/rfc8410.html#section-3
pub const Ed25519: &[u8] = &[0x06, 0x03, 0x2B, 0x65, 0x70];

/// The DER encoding of Ed448's OID.
///
/// Ed448's OID is 1.3.101.113 and is specified in [section 3 of RFC
/// 8410].
///
/// [section 3 of RFC 8410]: https://www.rfc-editor.org/rfc/rfc8410.html#section-3
pub const Ed448: &[u8] = &[0x06, 0x03, 0x2B, 0x65, 0x71];

/// Converts an OID to a string representing the algorithm or a hex
/// dump if the OID is not known.
///
/// This is only intended for display purposes.
pub fn oid_to_str(oid: &[u8]) -> Cow<str> {
    if oid == crate::oids::X25519 {
        Cow::Borrowed("X25519")
    } else if oid == crate::oids::X448 {
        Cow::Borrowed("X448")
    } else if oid == crate::oids::Ed25519 {
        Cow::Borrowed("Ed25519")
    } else if oid == crate::oids::Ed448 {
        Cow::Borrowed("Ed448")
    } else {
        format!("DER OID: {}",
                oid.iter()
                .map(|x| format!("{:02x}", x))
                .collect::<Vec<String>>()
                .join(" ")).into()
    }
}