Skip to content

Commit

Permalink
Merge pull request #526 from KodrAus/chore/v3-v5-deps
Browse files Browse the repository at this point in the history
Update dependencies for sha-1 and md-5
  • Loading branch information
KodrAus committed Aug 13, 2021
2 parents dde80b2 + 5df3d79 commit be6c7de
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 111 deletions.
41 changes: 0 additions & 41 deletions .github/workflows/clippy.yml

This file was deleted.

22 changes: 0 additions & 22 deletions .github/workflows/fmt.yml

This file was deleted.

13 changes: 6 additions & 7 deletions Cargo.toml
Expand Up @@ -55,20 +55,20 @@ repository = "uuid-rs/uuid"
optional = true
version = "0.2.0"

[dependencies.md5]
[dependencies.md-5]
default-features = false
optional = true
version = "0.7"
version = "0.9"

[dependencies.serde]
default-features = false
optional = true
version = "1.0.56"

[dependencies.sha1]
[dependencies.sha-1]
default-features = false
optional = true
version = "0.6"
version = "0.9"

[dependencies.slog]
optional = true
Expand Down Expand Up @@ -99,12 +99,11 @@ guid = ["winapi"]
std = []
stdweb = ["getrandom", "getrandom/js"]
v1 = []
v3 = ["md5"]
v3 = ["md-5"]
v4 = ["getrandom"]
v5 = ["sha1"]
v5 = ["sha-1"]
wasm-bindgen = ["getrandom", "getrandom/js"]

[target.'cfg(windows)'.dependencies.winapi]
optional = true
version = "0.3"

2 changes: 1 addition & 1 deletion src/adapter/mod.rs
Expand Up @@ -816,7 +816,7 @@ impl<'a> UrnRef<'a> {
/// [`Uuid`]: ../struct.Uuid.html
/// [`UrnRef`]: struct.UrnRef.html
pub const fn from_uuid_ref(uuid: &'a Uuid) -> Self {
UrnRef(&uuid)
UrnRef(uuid)
}

/// Writes the [`Uuid`] as a lower-case URN string to
Expand Down
8 changes: 4 additions & 4 deletions src/builder/mod.rs
Expand Up @@ -78,7 +78,7 @@ impl Uuid {
let len = d4.len();

if len != D4_LEN {
Err(Error::new(D4_LEN, len))?;
return crate::err(Error::new(D4_LEN, len));
}

Ok(Uuid::from_bytes([
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Uuid {
let len = d4.len();

if len != D4_LEN {
Err(Error::new(D4_LEN, len))?;
return crate::err(Error::new(D4_LEN, len));
}

Ok(Uuid::from_bytes([
Expand Down Expand Up @@ -265,7 +265,7 @@ impl Uuid {
let len = b.len();

if len != BYTES_LEN {
Err(Error::new(BYTES_LEN, len))?;
return crate::err(Error::new(BYTES_LEN, len));
}

let mut bytes: Bytes = [0; 16];
Expand Down Expand Up @@ -372,7 +372,7 @@ impl crate::Builder {
let len = b.len();

if len != BYTES_LEN {
Err(Error::new(BYTES_LEN, len))?;
return crate::err(Error::new(BYTES_LEN, len));
}

let mut bytes: crate::Bytes = [0; 16];
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Expand Up @@ -7,6 +7,10 @@ use crate::{builder, parser};
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Error(Inner);

pub(crate) fn err<T>(err: impl Into<Error>) -> Result<T, Error> {
Err(err.into())
}

// TODO: write tests for Error
// BODY: not immediately blocking, but should be covered for 1.0
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Expand Up @@ -164,7 +164,10 @@ mod v5;
#[cfg(all(windows, feature = "winapi"))]
mod winapi_support;

use crate::std::{convert, fmt, str};
use crate::{
error::err,
std::{convert, fmt, str},
};

pub use crate::error::Error;

Expand Down
32 changes: 16 additions & 16 deletions src/parser/mod.rs
Expand Up @@ -64,13 +64,13 @@ impl Uuid {
len,
&[adapter::Hyphenated::LENGTH, adapter::Simple::LENGTH],
) {
Err(Error::InvalidLength {
return crate::err(Error::InvalidLength {
expected: error::ExpectedLength::Any(&[
adapter::Hyphenated::LENGTH,
adapter::Simple::LENGTH,
]),
found: len,
})?;
});
}

// `digit` counts only hexadecimal digits, `i_char` counts all chars.
Expand All @@ -82,19 +82,19 @@ impl Uuid {
for (i_char, chr) in input.bytes().enumerate() {
if digit as usize >= adapter::Simple::LENGTH && group != 4 {
if group == 0 {
Err(Error::InvalidLength {
return crate::err(Error::InvalidLength {
expected: error::ExpectedLength::Any(&[
adapter::Hyphenated::LENGTH,
adapter::Simple::LENGTH,
]),
found: len,
})?;
});
}

Err(Error::InvalidGroupCount {
return crate::err(Error::InvalidGroupCount {
expected: error::ExpectedLength::Any(&[1, 5]),
found: group + 1,
})?;
});
}

if digit % 2 == 0 {
Expand All @@ -121,26 +121,26 @@ impl Uuid {
digit
};

Err(Error::InvalidGroupLength {
return crate::err(Error::InvalidGroupLength {
expected: error::ExpectedLength::Exact(
GROUP_LENS[group],
),
found: found as usize,
group,
})?;
});
}
// Next group, decrement digit, it is incremented again
// at the bottom.
group += 1;
digit -= 1;
}
_ => {
Err(Error::InvalidCharacter {
return crate::err(Error::InvalidCharacter {
expected: "0123456789abcdefABCDEF-",
found: input[i_char..].chars().next().unwrap(),
index: i_char,
urn: error::UrnPrefix::Optional,
})?;
});
}
}
} else {
Expand All @@ -161,21 +161,21 @@ impl Uuid {
digit
};

Err(Error::InvalidGroupLength {
return crate::err(Error::InvalidGroupLength {
expected: error::ExpectedLength::Exact(
GROUP_LENS[group],
),
found: found as usize,
group,
})?;
});
}
_ => {
Err(Error::InvalidCharacter {
return crate::err(Error::InvalidCharacter {
expected: "0123456789abcdefABCDEF-",
found: input[i_char..].chars().next().unwrap(),
index: i_char,
urn: error::UrnPrefix::Optional,
})?;
});
}
}
buffer[(digit / 2) as usize] = acc;
Expand All @@ -188,11 +188,11 @@ impl Uuid {
// BODY: this only needed until we switch to
// ParseError
if ACC_GROUP_LENS[4] as u8 != digit {
Err(Error::InvalidGroupLength {
return crate::err(Error::InvalidGroupLength {
expected: error::ExpectedLength::Exact(GROUP_LENS[4]),
found: (digit as usize - ACC_GROUP_LENS[3]),
group,
})?;
});
}

Ok(Uuid::from_bytes(buffer))
Expand Down
9 changes: 5 additions & 4 deletions src/prelude.rs
Expand Up @@ -32,15 +32,16 @@
//! [`Variant`]: ../enum.Variant.html
//! [`Version`]: ../enum.Version.html
//! [`Builder`]: ../builder/struct.Builder.html
//!
#![cfg_attr(feature = "v1",
doc = "
#![cfg_attr(
feature = "v1",
doc = "
[`uuid::v1`]`::{`[`ClockSequence`],[`Context`]`}`: The types useful for
handling uuid version 1. Requires feature `v1`.
[`uuid::v1`]: ../v1/index.html
[`Context`]: ../v1/struct.Context.html
[`ClockSequence`]: ../v1/trait.ClockSequence.html")]
[`ClockSequence`]: ../v1/trait.ClockSequence.html"
)]

pub use super::{Builder, Bytes, Error, Uuid, Variant, Version};
#[cfg(feature = "v1")]
Expand Down
2 changes: 1 addition & 1 deletion src/serde_support.rs
Expand Up @@ -20,7 +20,7 @@ impl Serialize for Uuid {
) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
serializer
.serialize_str(&self.to_hyphenated().encode_lower(&mut [0; 36]))
.serialize_str(self.to_hyphenated().encode_lower(&mut [0; 36]))
} else {
serializer.serialize_bytes(self.as_bytes())
}
Expand Down
1 change: 0 additions & 1 deletion src/slog_support.rs
Expand Up @@ -10,7 +10,6 @@
// except according to those terms.

use crate::prelude::*;
use slog;

impl slog::Value for Uuid {
fn serialize(
Expand Down
2 changes: 1 addition & 1 deletion src/v1.rs
Expand Up @@ -196,7 +196,7 @@ impl Uuid {

let len = node_id.len();
if len != NODE_ID_LEN {
Err(crate::builder::Error::new(NODE_ID_LEN, len))?;
return crate::err(crate::builder::Error::new(NODE_ID_LEN, len));
}

let time_low = (ts.ticks & 0xFFFF_FFFF) as u32;
Expand Down
15 changes: 8 additions & 7 deletions src/v3.rs
@@ -1,5 +1,5 @@
use crate::prelude::*;
use md5;
use md5::{Digest, Md5};

impl Uuid {
/// Creates a UUID using a name from a namespace, based on the MD5
Expand All @@ -20,16 +20,17 @@ impl Uuid {
/// [`NAMESPACE_URL`]: #associatedconstant.NAMESPACE_URL
/// [`NAMESPACE_X500`]: #associatedconstant.NAMESPACE_X500
pub fn new_v3(namespace: &Uuid, name: &[u8]) -> Uuid {
let mut context = md5::Context::new();
let mut hasher = Md5::new();

context.consume(namespace.as_bytes());
context.consume(name);
hasher.update(namespace.as_bytes());
hasher.update(name);

let computed = context.compute();
let bytes = computed.into();
let buffer = hasher.finalize();

let mut builder = crate::Builder::from_bytes(bytes);
let mut bytes = crate::Bytes::default();
bytes.copy_from_slice(&buffer[..16]);

let mut builder = crate::Builder::from_bytes(bytes);
builder
.set_variant(Variant::RFC4122)
.set_version(Version::Md5);
Expand Down
10 changes: 5 additions & 5 deletions src/v5.rs
@@ -1,5 +1,5 @@
use crate::prelude::*;
use sha1;
use sha1::{Digest, Sha1};

impl Uuid {
/// Creates a UUID using a name from a namespace, based on the SHA-1 hash.
Expand All @@ -19,12 +19,12 @@ impl Uuid {
/// [`NAMESPACE_URL`]: struct.Uuid.html#associatedconst.NAMESPACE_URL
/// [`NAMESPACE_X500`]: struct.Uuid.html#associatedconst.NAMESPACE_X500
pub fn new_v5(namespace: &Uuid, name: &[u8]) -> Uuid {
let mut hash = sha1::Sha1::new();
let mut hasher = Sha1::new();

hash.update(namespace.as_bytes());
hash.update(name);
hasher.update(namespace.as_bytes());
hasher.update(name);

let buffer = hash.digest().bytes();
let buffer = hasher.finalize();

let mut bytes = crate::Bytes::default();
bytes.copy_from_slice(&buffer[..16]);
Expand Down

0 comments on commit be6c7de

Please sign in to comment.