Debian Codegraph @

third_party/rust/uuid/src/v3.rs

raw
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
36 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(target_arch = "wasm32")]
46 use wasm_bindgen_test::*;
47
48 use crate::{std::string::ToString, Variant, Version};
49
50 static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
51 (
52 &Uuid::NAMESPACE_DNS,
53 "example.org",
54 "04738bdf-b25a-3829-a801-b21a1d25095b",
55 ),
56 (
57 &Uuid::NAMESPACE_DNS,
58 "rust-lang.org",
59 "c6db027c-615c-3b4d-959e-1a917747ca5a",
60 ),
61 (
62 &Uuid::NAMESPACE_DNS,
63 "42",
64 "5aab6e0c-b7d3-379c-92e3-2bfbb5572511",
65 ),
66 (
67 &Uuid::NAMESPACE_DNS,
68 "lorem ipsum",
69 "4f8772e9-b59c-3cc9-91a9-5c823df27281",
70 ),
71 (
72 &Uuid::NAMESPACE_URL,
73 "example.org",
74 "39682ca1-9168-3da2-a1bb-f4dbcde99bf9",
75 ),
76 (
77 &Uuid::NAMESPACE_URL,
78 "rust-lang.org",
79 "7ed45aaf-e75b-3130-8e33-ee4d9253b19f",
80 ),
81 (
82 &Uuid::NAMESPACE_URL,
83 "42",
84 "08998a0c-fcf4-34a9-b444-f2bfc15731dc",
85 ),
86 (
87 &Uuid::NAMESPACE_URL,
88 "lorem ipsum",
89 "e55ad2e6-fb89-34e8-b012-c5dde3cd67f0",
90 ),
91 (
92 &Uuid::NAMESPACE_OID,
93 "example.org",
94 "f14eec63-2812-3110-ad06-1625e5a4a5b2",
95 ),
96 (
97 &Uuid::NAMESPACE_OID,
98 "rust-lang.org",
99 "6506a0ec-4d79-3e18-8c2b-f2b6b34f2b6d",
100 ),
101 (
102 &Uuid::NAMESPACE_OID,
103 "42",
104 "ce6925a5-2cd7-327b-ab1c-4b375ac044e4",
105 ),
106 (
107 &Uuid::NAMESPACE_OID,
108 "lorem ipsum",
109 "5dd8654f-76ba-3d47-bc2e-4d6d3a78cb09",
110 ),
111 (
112 &Uuid::NAMESPACE_X500,
113 "example.org",
114 "64606f3f-bd63-363e-b946-fca13611b6f7",
115 ),
116 (
117 &Uuid::NAMESPACE_X500,
118 "rust-lang.org",
119 "bcee7a9c-52f1-30c6-a3cc-8c72ba634990",
120 ),
121 (
122 &Uuid::NAMESPACE_X500,
123 "42",
124 "c1073fa2-d4a6-3104-b21d-7a6bdcf39a23",
125 ),
126 (
127 &Uuid::NAMESPACE_X500,
128 "lorem ipsum",
129 "02f09a3f-1624-3b1d-8409-44eff7708208",
130 ),
131 ];
132
133 #[test]
134 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
135 fn test_new() {
136 for &(ref ns, ref name, _) in FIXTURE {
137 let uuid = Uuid::new_v3(*ns, name.as_bytes());
138 assert_eq!(uuid.get_version(), Some(Version::Md5));
139 assert_eq!(uuid.get_variant(), Variant::RFC4122);
140 }
141 }
142
143 #[test]
144 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
145 fn test_hyphenated_string() {
146 for &(ref ns, ref name, ref expected) in FIXTURE {
147 let uuid = Uuid::new_v3(*ns, name.as_bytes());
148 assert_eq!(uuid.hyphenated().to_string(), *expected);
149 }
150 }
151 }