1 use crate::Uuid; 2 3 impl Uuid { 4 /// Creates a UUID using a name from a namespace, based on the SHA-1 hash. 5 /// 6 /// A number of namespaces are available as constants in this crate: 7 /// 8 /// * [`NAMESPACE_DNS`] 9 /// * [`NAMESPACE_OID`] 10 /// * [`NAMESPACE_URL`] 11 /// * [`NAMESPACE_X500`] 12 /// 13 /// Note that usage of this method requires the `v5` feature of this crate 14 /// to be enabled. 15 /// 16 /// # Examples 17 /// 18 /// Generating a SHA1 DNS UUID for `rust-lang.org`: 19 /// 20 /// ``` 21 /// # use uuid::{Uuid, Version}; 22 /// let uuid = Uuid::new_v5(&Uuid::NAMESPACE_DNS, b"rust-lang.org"); 23 /// 24 /// assert_eq!(Some(Version::Sha1), uuid.get_version()); 25 /// ``` 26 /// 27 /// # References 28 /// 29 /// * [UUID Version 5 in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-5.5) 30 /// * [Name-Based UUID Generation in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-6.5) 31 /// 32 /// [`NAMESPACE_DNS`]: struct.Uuid.html#associatedconstant.NAMESPACE_DNS 33 /// [`NAMESPACE_OID`]: struct.Uuid.html#associatedconstant.NAMESPACE_OID 34 /// [`NAMESPACE_URL`]: struct.Uuid.html#associatedconstant.NAMESPACE_URL 35 /// [`NAMESPACE_X500`]: struct.Uuid.html#associatedconstant.NAMESPACE_X500 new_v5(namespace: &Uuid, name: &[u8]) -> Uuid36 pub fn new_v5(namespace: &Uuid, name: &[u8]) -> Uuid { 37 crate::Builder::from_sha1_bytes(crate::sha1::hash(namespace.as_bytes(), name)).into_uuid() 38 } 39 } 40 41 #[cfg(test)] 42 mod tests { 43 use super::*; 44 45 #[cfg(all( 46 target_arch = "wasm32", 47 target_vendor = "unknown", 48 target_os = "unknown" 49 ))] 50 use wasm_bindgen_test::*; 51 52 use crate::{std::string::ToString, Variant, Version}; 53 54 static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[ 55 ( 56 &Uuid::NAMESPACE_DNS, 57 "example.org", 58 "aad03681-8b63-5304-89e0-8ca8f49461b5", 59 ), 60 ( 61 &Uuid::NAMESPACE_DNS, 62 "rust-lang.org", 63 "c66bbb60-d62e-5f17-a399-3a0bd237c503", 64 ), 65 ( 66 &Uuid::NAMESPACE_DNS, 67 "42", 68 "7c411b5e-9d3f-50b5-9c28-62096e41c4ed", 69 ), 70 ( 71 &Uuid::NAMESPACE_DNS, 72 "lorem ipsum", 73 "97886a05-8a68-5743-ad55-56ab2d61cf7b", 74 ), 75 ( 76 &Uuid::NAMESPACE_URL, 77 "example.org", 78 "54a35416-963c-5dd6-a1e2-5ab7bb5bafc7", 79 ), 80 ( 81 &Uuid::NAMESPACE_URL, 82 "rust-lang.org", 83 "c48d927f-4122-5413-968c-598b1780e749", 84 ), 85 ( 86 &Uuid::NAMESPACE_URL, 87 "42", 88 "5c2b23de-4bad-58ee-a4b3-f22f3b9cfd7d", 89 ), 90 ( 91 &Uuid::NAMESPACE_URL, 92 "lorem ipsum", 93 "15c67689-4b85-5253-86b4-49fbb138569f", 94 ), 95 ( 96 &Uuid::NAMESPACE_OID, 97 "example.org", 98 "34784df9-b065-5094-92c7-00bb3da97a30", 99 ), 100 ( 101 &Uuid::NAMESPACE_OID, 102 "rust-lang.org", 103 "8ef61ecb-977a-5844-ab0f-c25ef9b8d5d6", 104 ), 105 ( 106 &Uuid::NAMESPACE_OID, 107 "42", 108 "ba293c61-ad33-57b9-9671-f3319f57d789", 109 ), 110 ( 111 &Uuid::NAMESPACE_OID, 112 "lorem ipsum", 113 "6485290d-f79e-5380-9e64-cb4312c7b4a6", 114 ), 115 ( 116 &Uuid::NAMESPACE_X500, 117 "example.org", 118 "e3635e86-f82b-5bbc-a54a-da97923e5c76", 119 ), 120 ( 121 &Uuid::NAMESPACE_X500, 122 "rust-lang.org", 123 "26c9c3e9-49b7-56da-8b9f-a0fb916a71a3", 124 ), 125 ( 126 &Uuid::NAMESPACE_X500, 127 "42", 128 "e4b88014-47c6-5fe0-a195-13710e5f6e27", 129 ), 130 ( 131 &Uuid::NAMESPACE_X500, 132 "lorem ipsum", 133 "b11f79a5-1e6d-57ce-a4b5-ba8531ea03d0", 134 ), 135 ]; 136 137 #[test] 138 #[cfg_attr( 139 all( 140 target_arch = "wasm32", 141 target_vendor = "unknown", 142 target_os = "unknown" 143 ), 144 wasm_bindgen_test 145 )] test_get_version()146 fn test_get_version() { 147 let uuid = Uuid::new_v5(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes()); 148 149 assert_eq!(uuid.get_version(), Some(Version::Sha1)); 150 assert_eq!(uuid.get_version_num(), 5); 151 } 152 153 #[test] 154 #[cfg_attr( 155 all( 156 target_arch = "wasm32", 157 target_vendor = "unknown", 158 target_os = "unknown" 159 ), 160 wasm_bindgen_test 161 )] test_hyphenated()162 fn test_hyphenated() { 163 for &(ref ns, ref name, ref expected) in FIXTURE { 164 let uuid = Uuid::new_v5(*ns, name.as_bytes()); 165 166 assert_eq!(uuid.hyphenated().to_string(), *expected) 167 } 168 } 169 170 #[test] 171 #[cfg_attr( 172 all( 173 target_arch = "wasm32", 174 target_vendor = "unknown", 175 target_os = "unknown" 176 ), 177 wasm_bindgen_test 178 )] test_new()179 fn test_new() { 180 for &(ref ns, ref name, ref u) in FIXTURE { 181 let uuid = Uuid::new_v5(*ns, name.as_bytes()); 182 183 assert_eq!(uuid.get_version(), Some(Version::Sha1)); 184 assert_eq!(uuid.get_variant(), Variant::RFC4122); 185 assert_eq!(Ok(uuid), u.parse()); 186 } 187 } 188 } 189