1 use crate::prelude::*; 2 use sha1; 3 4 impl Uuid { 5 /// Creates a UUID using a name from a namespace, based on the SHA-1 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 `v5` feature of this crate 15 /// to be enabled. 16 /// 17 /// [`NAMESPACE_DNS`]: struct.Uuid.html#associatedconst.NAMESPACE_DNS 18 /// [`NAMESPACE_OID`]: struct.Uuid.html#associatedconst.NAMESPACE_OID 19 /// [`NAMESPACE_URL`]: struct.Uuid.html#associatedconst.NAMESPACE_URL 20 /// [`NAMESPACE_X500`]: struct.Uuid.html#associatedconst.NAMESPACE_X500 new_v5(namespace: &Uuid, name: &[u8]) -> Uuid21 pub fn new_v5(namespace: &Uuid, name: &[u8]) -> Uuid { 22 let mut hash = sha1::Sha1::new(); 23 24 hash.update(namespace.as_bytes()); 25 hash.update(name); 26 27 let buffer = hash.digest().bytes(); 28 29 let mut bytes = crate::Bytes::default(); 30 bytes.copy_from_slice(&buffer[..16]); 31 32 let mut builder = crate::Builder::from_bytes(bytes); 33 builder 34 .set_variant(Variant::RFC4122) 35 .set_version(Version::Sha1); 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 "aad03681-8b63-5304-89e0-8ca8f49461b5", 52 ), 53 ( 54 &Uuid::NAMESPACE_DNS, 55 "rust-lang.org", 56 "c66bbb60-d62e-5f17-a399-3a0bd237c503", 57 ), 58 ( 59 &Uuid::NAMESPACE_DNS, 60 "42", 61 "7c411b5e-9d3f-50b5-9c28-62096e41c4ed", 62 ), 63 ( 64 &Uuid::NAMESPACE_DNS, 65 "lorem ipsum", 66 "97886a05-8a68-5743-ad55-56ab2d61cf7b", 67 ), 68 ( 69 &Uuid::NAMESPACE_URL, 70 "example.org", 71 "54a35416-963c-5dd6-a1e2-5ab7bb5bafc7", 72 ), 73 ( 74 &Uuid::NAMESPACE_URL, 75 "rust-lang.org", 76 "c48d927f-4122-5413-968c-598b1780e749", 77 ), 78 ( 79 &Uuid::NAMESPACE_URL, 80 "42", 81 "5c2b23de-4bad-58ee-a4b3-f22f3b9cfd7d", 82 ), 83 ( 84 &Uuid::NAMESPACE_URL, 85 "lorem ipsum", 86 "15c67689-4b85-5253-86b4-49fbb138569f", 87 ), 88 ( 89 &Uuid::NAMESPACE_OID, 90 "example.org", 91 "34784df9-b065-5094-92c7-00bb3da97a30", 92 ), 93 ( 94 &Uuid::NAMESPACE_OID, 95 "rust-lang.org", 96 "8ef61ecb-977a-5844-ab0f-c25ef9b8d5d6", 97 ), 98 ( 99 &Uuid::NAMESPACE_OID, 100 "42", 101 "ba293c61-ad33-57b9-9671-f3319f57d789", 102 ), 103 ( 104 &Uuid::NAMESPACE_OID, 105 "lorem ipsum", 106 "6485290d-f79e-5380-9e64-cb4312c7b4a6", 107 ), 108 ( 109 &Uuid::NAMESPACE_X500, 110 "example.org", 111 "e3635e86-f82b-5bbc-a54a-da97923e5c76", 112 ), 113 ( 114 &Uuid::NAMESPACE_X500, 115 "rust-lang.org", 116 "26c9c3e9-49b7-56da-8b9f-a0fb916a71a3", 117 ), 118 ( 119 &Uuid::NAMESPACE_X500, 120 "42", 121 "e4b88014-47c6-5fe0-a195-13710e5f6e27", 122 ), 123 ( 124 &Uuid::NAMESPACE_X500, 125 "lorem ipsum", 126 "b11f79a5-1e6d-57ce-a4b5-ba8531ea03d0", 127 ), 128 ]; 129 130 #[test] test_get_version()131 fn test_get_version() { 132 let uuid = 133 Uuid::new_v5(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes()); 134 135 assert_eq!(uuid.get_version(), Some(Version::Sha1)); 136 assert_eq!(uuid.get_version_num(), 5); 137 } 138 139 #[test] test_hyphenated()140 fn test_hyphenated() { 141 for &(ref ns, ref name, ref expected) in FIXTURE { 142 let uuid = Uuid::new_v5(*ns, name.as_bytes()); 143 144 assert_eq!(uuid.to_hyphenated().to_string(), *expected) 145 } 146 } 147 148 #[test] test_new()149 fn test_new() { 150 for &(ref ns, ref name, ref u) in FIXTURE { 151 let uuid = Uuid::new_v5(*ns, name.as_bytes()); 152 153 assert_eq!(uuid.get_variant(), Some(Variant::RFC4122)); 154 assert_eq!(uuid.get_version(), Some(Version::Sha1)); 155 assert_eq!(Ok(uuid), u.parse()); 156 } 157 } 158 } 159