Skip to content

Commit

Permalink
hash to get tags, make tagging a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
grimerssy committed Aug 18, 2024
1 parent 8c04a56 commit e0a1960
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ keywords = ["sql", "id", "encode", "obfuscate"]
categories = ["cryptography", "encoding", "no-std"]

[features]
default = ["std"]
default = ["std", "tag"]
std = []
tag = ["dep:const-fnv1a-hash"]
zeroize = ["aes/zeroize"]

[dependencies]
aes = "0.8.4"
const-fnv1a-hash = { version = "1.1.0", optional = true }

[dev-dependencies]
prop-test = "0.1.1"
Expand All @@ -28,3 +30,6 @@ opt-level = 3

[profile.test.package.rand_chacha]
opt-level = 3

[package.metadata."docs.rs"]
all-features = true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ let cipher = Aes128::new(&key.into());
let user_id = UserId::new(1, &cipher);
let crate_id = CrateId::new(1, &cipher);

assert_eq!("mC67zUocKZHWRXCHBr4aj52", &user_id.to_string());
assert_eq!("oPnmjU48WRy4GNradtqYdX2", &crate_id.to_string());
assert_eq!("qXfXkNN9ReZCGXu3qi28xC2", &user_id.to_string());
assert_eq!("VgtE1tzjDEHnjd3fh3PwiT2", &crate_id.to_string());
```

# Comparison
Expand Down
12 changes: 0 additions & 12 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ use crate::{
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Id<const TAG: u64>(u128);

pub const fn tag(tag: &str) -> u64 {
assert!(tag.len() <= 8, "tag is too long");
let mut byte_block = [0; 8];
let tag = tag.as_bytes();
let mut i = 0;
while i < tag.len() {
byte_block[i] = tag[i];
i += 1;
}
u64::from_be_bytes(byte_block)
}

impl<const TAG: u64> Id<TAG> {
pub fn new(id: i64, cipher: &Aes128) -> Self {
Self(encrypt(TAG, id, cipher))
Expand Down
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ mod encryption;
mod error;
mod id;

#[cfg(feature = "tag")]
mod tag;

pub use aes;

pub use self::{
error::Error,
id::{tag, Id},
};
pub use error::Error;
pub use id::Id;

#[cfg(feature = "tag")]
pub use tag::tag;

pub type Result<T> = core::result::Result<T, Error>;
3 changes: 3 additions & 0 deletions src/tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const fn tag(tag: &str) -> u64 {
const_fnv1a_hash::fnv1a_hash_str_64(tag)
}

0 comments on commit e0a1960

Please sign in to comment.