uuid/
v5.rs

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