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