pub enum Cipher {
Aes128Cbc,
Aes256Cbc,
Aes128Gcm,
Aes256Gcm,
}Expand description
A cipher supported by Laravel’s encrypter.
Variants§
Aes128Cbc
aes-128-cbc
Aes256Cbc
aes-256-cbc, Laravel’s default.
Aes128Gcm
aes-128-gcm
Aes256Gcm
aes-256-gcm
Implementations§
Source§impl Cipher
impl Cipher
Sourcepub fn name(self) -> &'static str
pub fn name(self) -> &'static str
The cipher’s name as used in Laravel’s config/app.php, in lower case.
Examples found in repository?
examples/genvectors.rs (line 119)
105fn main() {
106 let mut vectors = Vec::new();
107 for cipher in [
108 Cipher::Aes128Cbc,
109 Cipher::Aes256Cbc,
110 Cipher::Aes128Gcm,
111 Cipher::Aes256Gcm,
112 ] {
113 let enc = Encrypter::new(&KEY[..cipher.key_size()], cipher).expect("valid key");
114
115 for (name, plaintext) in strings() {
116 vectors.push(json!({
117 "kind": "string",
118 "name": format!("{cipher}/{name}"),
119 "cipher": cipher.name(),
120 "plaintext_b64": base64(&plaintext),
121 "payload": enc.encrypt_bytes(&plaintext).expect("encrypt"),
122 }));
123 }
124 for (name, value) in cases() {
125 vectors.push(json!({
126 "kind": "serialized",
127 "name": format!("{cipher}/{name}"),
128 "case": name,
129 "cipher": cipher.name(),
130 "payload": enc.encrypt(&value).expect("encrypt"),
131 }));
132 }
133 }
134
135 println!("{}", json!({ "key_b64": base64(&KEY), "vectors": vectors }));
136}More examples
examples/mutations.rs (line 100)
29fn main() {
30 let mut state = 0x1234_5678_9abc_def1_u64;
31 let mut next = move || {
32 state ^= state << 13;
33 state ^= state >> 7;
34 state ^= state << 17;
35 state
36 };
37 let encode = |j: &Value| STANDARD.encode(serde_json::to_vec(j).expect("serialize"));
38
39 let mut cases = Vec::new();
40 for cipher in [
41 Cipher::Aes128Cbc,
42 Cipher::Aes256Cbc,
43 Cipher::Aes128Gcm,
44 Cipher::Aes256Gcm,
45 ] {
46 let enc = Encrypter::new(&vec![0x2b; cipher.key_size()], cipher).expect("valid key");
47
48 for text in ["", "hello", "0123456789abcdef"] {
49 let valid = enc.encrypt_string(text).expect("encrypt");
50 let base: Value =
51 serde_json::from_slice(&STANDARD.decode(&valid).expect("base64")).expect("json");
52
53 let mut payloads = vec![
54 valid.clone(),
55 format!(" {valid}\n"),
56 valid.trim_end_matches('=').to_owned(),
57 ];
58
59 // Field-level edits: odd values, removal, upper case, an extra character.
60 for field in ["iv", "value", "mac", "tag"] {
61 for odd in odd_values() {
62 let mut j = base.clone();
63 j[field] = odd;
64 payloads.push(encode(&j));
65 }
66 let mut j = base.clone();
67 j.as_object_mut().expect("object").remove(field);
68 payloads.push(encode(&j));
69
70 let text = base[field].as_str().expect("string").to_owned();
71 let mut j = base.clone();
72 j[field] = json!(text.to_uppercase());
73 payloads.push(encode(&j));
74 let mut j = base.clone();
75 j[field] = json!(format!("{text}A"));
76 payloads.push(encode(&j));
77 }
78
79 // Byte-level edits of the outer base64 text.
80 for _ in 0..600 {
81 let mut b = valid.clone().into_bytes();
82 let at = next() as usize % b.len();
83 match next() % 5 {
84 0 => b[at] = b"ABCxyz09+/=!\n "[next() as usize % 14],
85 1 => {
86 b.remove(at);
87 }
88 2 => b.truncate(at),
89 3 => b.insert(at, b" \n=*"[next() as usize % 4]),
90 _ => {
91 let other = next() as usize % b.len();
92 b.swap(at, other);
93 }
94 }
95 payloads.push(String::from_utf8_lossy(&b).into_owned());
96 }
97
98 for payload in payloads {
99 let plain = enc.decrypt_string(&payload).ok();
100 cases.push(json!({ "cipher": cipher.name(), "payload": payload, "plain": plain }));
101 }
102 }
103 }
104
105 // A closed pipe (PHP exited early) is not an error worth a panic.
106 let _ = writeln!(
107 std::io::stdout(),
108 "{}",
109 json!({ "key_b64": STANDARD.encode([0x2b; 32]), "cases": cases })
110 );
111}Sourcepub fn key_size(self) -> usize
pub fn key_size(self) -> usize
The key length this cipher requires, in bytes.
Examples found in repository?
examples/genvectors.rs (line 113)
105fn main() {
106 let mut vectors = Vec::new();
107 for cipher in [
108 Cipher::Aes128Cbc,
109 Cipher::Aes256Cbc,
110 Cipher::Aes128Gcm,
111 Cipher::Aes256Gcm,
112 ] {
113 let enc = Encrypter::new(&KEY[..cipher.key_size()], cipher).expect("valid key");
114
115 for (name, plaintext) in strings() {
116 vectors.push(json!({
117 "kind": "string",
118 "name": format!("{cipher}/{name}"),
119 "cipher": cipher.name(),
120 "plaintext_b64": base64(&plaintext),
121 "payload": enc.encrypt_bytes(&plaintext).expect("encrypt"),
122 }));
123 }
124 for (name, value) in cases() {
125 vectors.push(json!({
126 "kind": "serialized",
127 "name": format!("{cipher}/{name}"),
128 "case": name,
129 "cipher": cipher.name(),
130 "payload": enc.encrypt(&value).expect("encrypt"),
131 }));
132 }
133 }
134
135 println!("{}", json!({ "key_b64": base64(&KEY), "vectors": vectors }));
136}More examples
examples/mutations.rs (line 46)
29fn main() {
30 let mut state = 0x1234_5678_9abc_def1_u64;
31 let mut next = move || {
32 state ^= state << 13;
33 state ^= state >> 7;
34 state ^= state << 17;
35 state
36 };
37 let encode = |j: &Value| STANDARD.encode(serde_json::to_vec(j).expect("serialize"));
38
39 let mut cases = Vec::new();
40 for cipher in [
41 Cipher::Aes128Cbc,
42 Cipher::Aes256Cbc,
43 Cipher::Aes128Gcm,
44 Cipher::Aes256Gcm,
45 ] {
46 let enc = Encrypter::new(&vec![0x2b; cipher.key_size()], cipher).expect("valid key");
47
48 for text in ["", "hello", "0123456789abcdef"] {
49 let valid = enc.encrypt_string(text).expect("encrypt");
50 let base: Value =
51 serde_json::from_slice(&STANDARD.decode(&valid).expect("base64")).expect("json");
52
53 let mut payloads = vec![
54 valid.clone(),
55 format!(" {valid}\n"),
56 valid.trim_end_matches('=').to_owned(),
57 ];
58
59 // Field-level edits: odd values, removal, upper case, an extra character.
60 for field in ["iv", "value", "mac", "tag"] {
61 for odd in odd_values() {
62 let mut j = base.clone();
63 j[field] = odd;
64 payloads.push(encode(&j));
65 }
66 let mut j = base.clone();
67 j.as_object_mut().expect("object").remove(field);
68 payloads.push(encode(&j));
69
70 let text = base[field].as_str().expect("string").to_owned();
71 let mut j = base.clone();
72 j[field] = json!(text.to_uppercase());
73 payloads.push(encode(&j));
74 let mut j = base.clone();
75 j[field] = json!(format!("{text}A"));
76 payloads.push(encode(&j));
77 }
78
79 // Byte-level edits of the outer base64 text.
80 for _ in 0..600 {
81 let mut b = valid.clone().into_bytes();
82 let at = next() as usize % b.len();
83 match next() % 5 {
84 0 => b[at] = b"ABCxyz09+/=!\n "[next() as usize % 14],
85 1 => {
86 b.remove(at);
87 }
88 2 => b.truncate(at),
89 3 => b.insert(at, b" \n=*"[next() as usize % 4]),
90 _ => {
91 let other = next() as usize % b.len();
92 b.swap(at, other);
93 }
94 }
95 payloads.push(String::from_utf8_lossy(&b).into_owned());
96 }
97
98 for payload in payloads {
99 let plain = enc.decrypt_string(&payload).ok();
100 cases.push(json!({ "cipher": cipher.name(), "payload": payload, "plain": plain }));
101 }
102 }
103 }
104
105 // A closed pipe (PHP exited early) is not an error worth a panic.
106 let _ = writeln!(
107 std::io::stdout(),
108 "{}",
109 json!({ "key_b64": STANDARD.encode([0x2b; 32]), "cases": cases })
110 );
111}Trait Implementations§
impl Copy for Cipher
impl Eq for Cipher
impl StructuralPartialEq for Cipher
Auto Trait Implementations§
impl Freeze for Cipher
impl RefUnwindSafe for Cipher
impl Send for Cipher
impl Sync for Cipher
impl Unpin for Cipher
impl UnsafeUnpin for Cipher
impl UnwindSafe for Cipher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more