uuid/
v3.rs

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