Debian Codegraph @

src/message/password.rs

raw symbols SCIP
1 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::iorust-analyzer cargo sqlx-postgres 0.8.3 io/::BufMutExtrust-analyzer cargo sqlx-core 0.8.3 io/buf_mut/BufMutExt#;
2 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::messagerust-analyzer cargo sqlx-postgres 0.8.3 message/::{FrontendMessagerust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessage#, FrontendMessageFormatrust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessageFormat#};
3 use md5rust-analyzer cargo md-5 0.10.6 crate/::{Digestrust-analyzer cargo digest 0.10.7 digest/Digest#, Md5rust-analyzer cargo md-5 0.10.6 Md5#};
4 use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#;
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::Saturatingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/Saturating#;
7
8 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
9 pub enum Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#<'a> {
10 Cleartextrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Cleartext#(&'a str),
11
12 Md5rust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5# {
13 passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#password.: &'a str,
14 usernamerust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#username.: &'a str,
15 saltrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#salt.: [u8; 4],
16 },
17 }
18
19 impl FrontendMessagerust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessage# for Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#<'_> {
20 const FORMATrust-analyzer cargo sqlx-postgres 0.8.3 message/password/impl#[`Password<'_>`][FrontendMessage]FORMAT.: FrontendMessageFormatrust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessageFormat# = FrontendMessageFormatrust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessageFormat#::PasswordPolymorphicrust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessageFormat#PasswordPolymorphic#;
21
22 #[inline(always)]
23 fn body_size_hintrust-analyzer cargo sqlx-postgres 0.8.3 message/password/impl#[`Password<'_>`][FrontendMessage]body_size_hint().(&self) -> Saturatingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/Saturating#<usize> {
24 let mut size = Saturatingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/Saturating#(0);
25
26 match self {
27 Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Cleartextrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Cleartext#(password) => {
28 // To avoid reporting the exact password length anywhere,
29 // we deliberately give a bad estimate.
30 //
31 // This shouldn't affect performance in the long run.
32 size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/impl#[`Saturating<usize>`][`AddAssign<usize>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/impl#[`Saturating<usize>`][`AddAssign<usize>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/impl#[`Saturating<usize>`][`AddAssign<usize>`]add_assign().password
33 .lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()
34 .saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(1) // NUL terminator
35 .checked_next_power_of_tworust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_next_power_of_two().()
36 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX.);
37 }
38 Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Md5rust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5# { .. } => {
39 // "md5<32 hex chars>\0"
40 size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/impl#[`Saturating<usize>`][`AddAssign<usize>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/impl#[`Saturating<usize>`][`AddAssign<usize>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/saturating/impl#[`Saturating<usize>`][`AddAssign<usize>`]add_assign().36;
41 }
42 }
43
44 size
45 }
46
47 fn encode_bodyrust-analyzer cargo sqlx-postgres 0.8.3 message/password/impl#[`Password<'_>`][FrontendMessage]encode_body().(&self, buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
48 match self {
49 Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Cleartextrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Cleartext#(password) => {
50 buf.put_str_nulrust-analyzer cargo sqlx-core 0.8.3 io/buf_mut/impl#[`Vec<u8>`][BufMutExt]put_str_nul().(password);
51 }
52
53 Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Md5rust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5# {
54 usernamerust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#username.,
55 passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#password.,
56 saltrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#salt.,
57 } => {
58 // The actual `PasswordMessage` can be computed in SQL as
59 // `concat('md5', md5(concat(md5(concat(password, username)), random-salt)))`.
60
61 // Keep in mind the md5() function returns its result as a hex string.
62
63 let mut hasher = Md5rust-analyzer cargo md-5 0.10.6 Md5#::newrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]new().();
64
65 hasher.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(password);
66 hasher.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(username);
67
68 let mut output = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(35);
69
70 let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(output, "{:x}", hasher.finalize_resetrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]finalize_reset().());
71
72 hasher.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(&output);
73 hasher.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(salt);
74
75 output.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().();
76
77 let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(output, "md5{:x}", hasher.finalizerust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]finalize().());
78
79 buf.put_str_nulrust-analyzer cargo sqlx-core 0.8.3 io/buf_mut/impl#[`Vec<u8>`][BufMutExt]put_str_nul().(&output);
80 }
81 }
82
83 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
84 }
85 }
86
87 #[cfg(test)]
88 mod testsrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/ {
89 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::messagerust-analyzer cargo sqlx-postgres 0.8.3 message/::FrontendMessagerust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessage#;
90
91 use superrust-analyzer cargo sqlx-postgres 0.8.3 message/password/::Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#;
92
93 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
94 fn test_encode_clear_passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/test_encode_clear_password().() {
95 const EXPECTEDrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/EXPECTED.: &[u8] = b"p\0\0\0\rpassword\0";
96
97 let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
98 let m = Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Cleartextrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Cleartext#("password");
99
100 m.encode_msgrust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessage#encode_msg().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
101
102 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf, EXPECTEDrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/EXPECTED.);
103 }
104
105 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
106 fn test_encode_md5_passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/test_encode_md5_password().() {
107 const EXPECTEDrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/EXPECTED.: &[u8] = b"p\0\0\0(md53e2c9d99d49b201ef867a36f3f9ed62c\0";
108
109 let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
110 let m = Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Md5rust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5# {
111 passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#password.: "password",
112 usernamerust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#username.: "root",
113 saltrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5#salt.: [147, 24, 57, 152],
114 };
115
116 m.encode_msgrust-analyzer cargo sqlx-postgres 0.8.3 message/FrontendMessage#encode_msg().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
117
118 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf, EXPECTEDrust-analyzer cargo sqlx-postgres 0.8.3 message/password/tests/EXPECTED.);
119 }
120
121 #[cfg(all(test, not(debug_assertions)))]
122 #[benchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/bench:]
123 fn bench_encode_clear_password(b: &mut test::Bencher) {
124 use test::black_box;
125
126 let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacity(128);
127
128 b.iter(|| {
129 buf.clear();
130
131 black_box(Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Cleartextrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Cleartext#("password")).encode_msg(&mut buf);
132 });
133 }
134
135 #[cfg(all(test, not(debug_assertions)))]
136 #[benchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/bench:]
137 fn bench_encode_md5_password(b: &mut test::Bencher) {
138 use test::black_box;
139
140 let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacity(128);
141
142 b.iter(|| {
143 buf.clear();
144
145 black_box(Passwordrust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#::Md5rust-analyzer cargo sqlx-postgres 0.8.3 message/password/Password#Md5# {
146 password: "password",
147 username: "root",
148 salt: [147, 24, 57, 152],
149 })
150 .encode_msg(&mut buf);
151 });
152 }
153 }