1 use crate::Uuid; 2 3 impl Uuid { 4 /// Creates a UUID using a name from a namespace, based on the MD5 5 /// hash. 6 /// 7 /// A number of namespaces are available as constants in this crate: 8 /// 9 /// * [`NAMESPACE_DNS`] 10 /// * [`NAMESPACE_OID`] 11 /// * [`NAMESPACE_URL`] 12 /// * [`NAMESPACE_X500`] 13 /// 14 /// Note that usage of this method requires the `v3` feature of this crate 15 /// to be enabled. 16 /// 17 /// # Examples 18 /// 19 /// Generating a MD5 DNS UUID for `rust-lang.org`: 20 /// 21 /// ``` 22 /// # use uuid::{Uuid, Version}; 23 /// let uuid = Uuid::new_v3(&Uuid::NAMESPACE_DNS, b"rust-lang.org"); 24 /// 25 /// assert_eq!(Some(Version::Md5), uuid.get_version()); 26 /// ``` 27 /// 28 /// # References 29 /// 30 /// * [Version 3 and 5 UUIDs in RFC4122](https://www.rfc-editor.org/rfc/rfc4122#section-4.3) 31 /// 32 /// [`NAMESPACE_DNS`]: #associatedconstant.NAMESPACE_DNS 33 /// [`NAMESPACE_OID`]: #associatedconstant.NAMESPACE_OID 34 /// [`NAMESPACE_URL`]: #associatedconstant.NAMESPACE_URL 35 /// [`NAMESPACE_X500`]: #associatedconstant.NAMESPACE_X500 new_v3(namespace: &Uuid, name: &[u8]) -> Uuid36 pub fn new_v3(namespace: &Uuid, name: &[u8]) -> Uuid { 37 crate::Builder::from_md5_bytes(crate::md5::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 "04738bdf-b25a-3829-a801-b21a1d25095b", 59 ), 60 ( 61 &Uuid::NAMESPACE_DNS, 62 "rust-lang.org", 63 "c6db027c-615c-3b4d-959e-1a917747ca5a", 64 ), 65 ( 66 &Uuid::NAMESPACE_DNS, 67 "42", 68 "5aab6e0c-b7d3-379c-92e3-2bfbb5572511", 69 ), 70 ( 71 &Uuid::NAMESPACE_DNS, 72 "lorem ipsum", 73 "4f8772e9-b59c-3cc9-91a9-5c823df27281", 74 ), 75 ( 76 &Uuid::NAMESPACE_URL, 77 "example.org", 78 "39682ca1-9168-3da2-a1bb-f4dbcde99bf9", 79 ), 80 ( 81 &Uuid::NAMESPACE_URL, 82 "rust-lang.org", 83 "7ed45aaf-e75b-3130-8e33-ee4d9253b19f", 84 ), 85 ( 86 &Uuid::NAMESPACE_URL, 87 "42", 88 "08998a0c-fcf4-34a9-b444-f2bfc15731dc", 89 ), 90 ( 91 &Uuid::NAMESPACE_URL, 92 "lorem ipsum", 93 "e55ad2e6-fb89-34e8-b012-c5dde3cd67f0", 94 ), 95 ( 96 &Uuid::NAMESPACE_OID, 97 "example.org", 98 "f14eec63-2812-3110-ad06-1625e5a4a5b2", 99 ), 100 ( 101 &Uuid::NAMESPACE_OID, 102 "rust-lang.org", 103 "6506a0ec-4d79-3e18-8c2b-f2b6b34f2b6d", 104 ), 105 ( 106 &Uuid::NAMESPACE_OID, 107 "42", 108 "ce6925a5-2cd7-327b-ab1c-4b375ac044e4", 109 ), 110 ( 111 &Uuid::NAMESPACE_OID, 112 "lorem ipsum", 113 "5dd8654f-76ba-3d47-bc2e-4d6d3a78cb09", 114 ), 115 ( 116 &Uuid::NAMESPACE_X500, 117 "example.org", 118 "64606f3f-bd63-363e-b946-fca13611b6f7", 119 ), 120 ( 121 &Uuid::NAMESPACE_X500, 122 "rust-lang.org", 123 "bcee7a9c-52f1-30c6-a3cc-8c72ba634990", 124 ), 125 ( 126 &Uuid::NAMESPACE_X500, 127 "42", 128 "c1073fa2-d4a6-3104-b21d-7a6bdcf39a23", 129 ), 130 ( 131 &Uuid::NAMESPACE_X500, 132 "lorem ipsum", 133 "02f09a3f-1624-3b1d-8409-44eff7708208", 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_new()146 fn test_new() { 147 for &(ref ns, ref name, _) in FIXTURE { 148 let uuid = Uuid::new_v3(*ns, name.as_bytes()); 149 assert_eq!(uuid.get_version(), Some(Version::Md5)); 150 assert_eq!(uuid.get_variant(), Variant::RFC4122); 151 } 152 } 153 154 #[test] 155 #[cfg_attr( 156 all( 157 target_arch = "wasm32", 158 target_vendor = "unknown", 159 target_os = "unknown" 160 ), 161 wasm_bindgen_test 162 )] test_hyphenated_string()163 fn test_hyphenated_string() { 164 for &(ref ns, ref name, ref expected) in FIXTURE { 165 let uuid = Uuid::new_v3(*ns, name.as_bytes()); 166 assert_eq!(uuid.hyphenated().to_string(), *expected); 167 } 168 } 169 } 170