1 use crate::prelude::*; 2 use md5; 3 4 impl Uuid { 5 /// Creates a UUID using a name from a namespace, based on the MD5 6 /// hash. 7 /// 8 /// A number of namespaces are available as constants in this crate: 9 /// 10 /// * [`NAMESPACE_DNS`] 11 /// * [`NAMESPACE_OID`] 12 /// * [`NAMESPACE_URL`] 13 /// * [`NAMESPACE_X500`] 14 /// 15 /// Note that usage of this method requires the `v3` feature of this crate 16 /// to be enabled. 17 /// 18 /// [`NAMESPACE_DNS`]: #associatedconstant.NAMESPACE_DNS 19 /// [`NAMESPACE_OID`]: #associatedconstant.NAMESPACE_OID 20 /// [`NAMESPACE_URL`]: #associatedconstant.NAMESPACE_URL 21 /// [`NAMESPACE_X500`]: #associatedconstant.NAMESPACE_X500 new_v3(namespace: &Uuid, name: &[u8]) -> Uuid22 pub fn new_v3(namespace: &Uuid, name: &[u8]) -> Uuid { 23 let mut context = md5::Context::new(); 24 25 context.consume(namespace.as_bytes()); 26 context.consume(name); 27 28 let computed = context.compute(); 29 let bytes = computed.into(); 30 31 let mut builder = crate::Builder::from_bytes(bytes); 32 33 builder 34 .set_variant(Variant::RFC4122) 35 .set_version(Version::Md5); 36 37 builder.build() 38 } 39 } 40 41 #[cfg(test)] 42 mod tests { 43 use super::*; 44 45 use crate::std::string::ToString; 46 47 static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[ 48 ( 49 &Uuid::NAMESPACE_DNS, 50 "example.org", 51 "04738bdf-b25a-3829-a801-b21a1d25095b", 52 ), 53 ( 54 &Uuid::NAMESPACE_DNS, 55 "rust-lang.org", 56 "c6db027c-615c-3b4d-959e-1a917747ca5a", 57 ), 58 ( 59 &Uuid::NAMESPACE_DNS, 60 "42", 61 "5aab6e0c-b7d3-379c-92e3-2bfbb5572511", 62 ), 63 ( 64 &Uuid::NAMESPACE_DNS, 65 "lorem ipsum", 66 "4f8772e9-b59c-3cc9-91a9-5c823df27281", 67 ), 68 ( 69 &Uuid::NAMESPACE_URL, 70 "example.org", 71 "39682ca1-9168-3da2-a1bb-f4dbcde99bf9", 72 ), 73 ( 74 &Uuid::NAMESPACE_URL, 75 "rust-lang.org", 76 "7ed45aaf-e75b-3130-8e33-ee4d9253b19f", 77 ), 78 ( 79 &Uuid::NAMESPACE_URL, 80 "42", 81 "08998a0c-fcf4-34a9-b444-f2bfc15731dc", 82 ), 83 ( 84 &Uuid::NAMESPACE_URL, 85 "lorem ipsum", 86 "e55ad2e6-fb89-34e8-b012-c5dde3cd67f0", 87 ), 88 ( 89 &Uuid::NAMESPACE_OID, 90 "example.org", 91 "f14eec63-2812-3110-ad06-1625e5a4a5b2", 92 ), 93 ( 94 &Uuid::NAMESPACE_OID, 95 "rust-lang.org", 96 "6506a0ec-4d79-3e18-8c2b-f2b6b34f2b6d", 97 ), 98 ( 99 &Uuid::NAMESPACE_OID, 100 "42", 101 "ce6925a5-2cd7-327b-ab1c-4b375ac044e4", 102 ), 103 ( 104 &Uuid::NAMESPACE_OID, 105 "lorem ipsum", 106 "5dd8654f-76ba-3d47-bc2e-4d6d3a78cb09", 107 ), 108 ( 109 &Uuid::NAMESPACE_X500, 110 "example.org", 111 "64606f3f-bd63-363e-b946-fca13611b6f7", 112 ), 113 ( 114 &Uuid::NAMESPACE_X500, 115 "rust-lang.org", 116 "bcee7a9c-52f1-30c6-a3cc-8c72ba634990", 117 ), 118 ( 119 &Uuid::NAMESPACE_X500, 120 "42", 121 "c1073fa2-d4a6-3104-b21d-7a6bdcf39a23", 122 ), 123 ( 124 &Uuid::NAMESPACE_X500, 125 "lorem ipsum", 126 "02f09a3f-1624-3b1d-8409-44eff7708208", 127 ), 128 ]; 129 130 #[test] test_new()131 fn test_new() { 132 for &(ref ns, ref name, _) in FIXTURE { 133 let uuid = Uuid::new_v3(*ns, name.as_bytes()); 134 assert_eq!(uuid.get_version().unwrap(), Version::Md5); 135 assert_eq!(uuid.get_variant().unwrap(), Variant::RFC4122); 136 } 137 } 138 139 #[test] test_to_hyphenated_string()140 fn test_to_hyphenated_string() { 141 for &(ref ns, ref name, ref expected) in FIXTURE { 142 let uuid = Uuid::new_v3(*ns, name.as_bytes()); 143 assert_eq!(uuid.to_hyphenated().to_string(), *expected); 144 } 145 } 146 } 147