Debian Codegraph @

.cargo/vendor/uuid/src/v5.rs

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