Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sec1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sec1"
version = "0.2.0" # Also update html_root_url in lib.rs when bumping this
version = "0.2.0-pre" # Also update html_root_url in lib.rs when bumping this
description = """
Pure Rust implementation of SEC1: Elliptic Curve Cryptography encoding formats
including ASN.1 DER-serialized private keys as well as the
Expand Down
6 changes: 3 additions & 3 deletions sec1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_root_url = "https://docs.rs/sec1/0.1.0"
html_root_url = "https://docs.rs/sec1/0.2.0-pre"
)]
#![forbid(unsafe_code, clippy::unwrap_used)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
Expand All @@ -37,13 +37,13 @@ pub use self::{
parameters::EcParameters,
point::EncodedPoint,
private_key::EcPrivateKey,
traits::FromEcPrivateKey,
traits::DecodeEcPrivateKey,
};

pub use generic_array::typenum::consts;

#[cfg(feature = "alloc")]
pub use crate::{private_key::document::EcPrivateKeyDocument, traits::ToEcPrivateKey};
pub use crate::{private_key::document::EcPrivateKeyDocument, traits::EncodeEcPrivateKey};

#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
Expand Down
14 changes: 7 additions & 7 deletions sec1/src/private_key/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! SEC1 EC private key document.

use crate::{EcPrivateKey, Error, FromEcPrivateKey, Result, ToEcPrivateKey};
use crate::{DecodeEcPrivateKey, EcPrivateKey, EncodeEcPrivateKey, Error, Result};
use alloc::{borrow::ToOwned, vec::Vec};
use core::{convert::TryFrom, fmt};
use der::{Decodable, Encodable};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl EcPrivateKeyDocument {
}
}

impl FromEcPrivateKey for EcPrivateKeyDocument {
impl DecodeEcPrivateKey for EcPrivateKeyDocument {
fn from_sec1_der(bytes: &[u8]) -> Result<Self> {
// Ensure document is well-formed
EcPrivateKey::from_der(bytes)?;
Expand All @@ -63,19 +63,19 @@ impl FromEcPrivateKey for EcPrivateKeyDocument {

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_sec1_der_file(path: &Path) -> Result<Self> {
fn read_sec1_der_file(path: impl AsRef<Path>) -> Result<Self> {
fs::read(path)?.try_into()
}

#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_sec1_pem_file(path: &Path) -> Result<Self> {
fn read_sec1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
Self::from_sec1_pem(&Zeroizing::new(fs::read_to_string(path)?))
}
}

impl ToEcPrivateKey for EcPrivateKeyDocument {
impl EncodeEcPrivateKey for EcPrivateKeyDocument {
fn to_sec1_der(&self) -> Result<EcPrivateKeyDocument> {
Ok(self.clone())
}
Expand All @@ -89,14 +89,14 @@ impl ToEcPrivateKey for EcPrivateKeyDocument {

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn write_sec1_der_file(&self, path: &Path) -> Result<()> {
fn write_sec1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
write_secret_file(path, self.as_der())
}

#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn write_sec1_pem_file(&self, path: &Path, line_ending: LineEnding) -> Result<()> {
fn write_sec1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
let pem_doc = self.to_sec1_pem(line_ending)?;
write_secret_file(path, pem_doc.as_bytes())
}
Expand Down
12 changes: 6 additions & 6 deletions sec1/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::path::Path;
use zeroize::Zeroizing;

/// Parse an [`EcPrivateKey`] from a SEC1-encoded document.
pub trait FromEcPrivateKey: for<'a> TryFrom<EcPrivateKey<'a>, Error = Error> + Sized {
pub trait DecodeEcPrivateKey: for<'a> TryFrom<EcPrivateKey<'a>, Error = Error> + Sized {
/// Deserialize SEC1 private key from ASN.1 DER-encoded data
/// (binary format).
fn from_sec1_der(bytes: &[u8]) -> Result<Self> {
Expand All @@ -41,7 +41,7 @@ pub trait FromEcPrivateKey: for<'a> TryFrom<EcPrivateKey<'a>, Error = Error> + S
/// filesystem (binary format).
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_sec1_der_file(path: &Path) -> Result<Self> {
fn read_sec1_der_file(path: impl AsRef<Path>) -> Result<Self> {
EcPrivateKeyDocument::read_sec1_der_file(path)
.and_then(|doc| Self::try_from(doc.private_key()))
}
Expand All @@ -50,7 +50,7 @@ pub trait FromEcPrivateKey: for<'a> TryFrom<EcPrivateKey<'a>, Error = Error> + S
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_sec1_pem_file(path: &Path) -> Result<Self> {
fn read_sec1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
EcPrivateKeyDocument::read_sec1_pem_file(path)
.and_then(|doc| Self::try_from(doc.private_key()))
}
Expand All @@ -59,7 +59,7 @@ pub trait FromEcPrivateKey: for<'a> TryFrom<EcPrivateKey<'a>, Error = Error> + S
/// Serialize a [`EcPrivateKey`] to a SEC1 encoded document.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub trait ToEcPrivateKey {
pub trait EncodeEcPrivateKey {
/// Serialize a [`EcPrivateKeyDocument`] containing a SEC1-encoded private key.
fn to_sec1_der(&self) -> Result<EcPrivateKeyDocument>;

Expand All @@ -75,15 +75,15 @@ pub trait ToEcPrivateKey {
/// Write ASN.1 DER-encoded SEC1 private key to the given path.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn write_sec1_der_file(&self, path: &Path) -> Result<()> {
fn write_sec1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
self.to_sec1_der()?.write_sec1_der_file(path)
}

/// Write ASN.1 DER-encoded SEC1 private key to the given path.
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn write_sec1_pem_file(&self, path: &Path, line_ending: LineEnding) -> Result<()> {
fn write_sec1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
self.to_sec1_der()?.write_sec1_pem_file(path, line_ending)
}
}