Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CSE-108c Project 1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cheuk Pui Lam
CSE-108c Project 1
Commits
ca0b5ed8
Unverified
Commit
ca0b5ed8
authored
1 month ago
by
Cheuk Pui Lam
Browse files
Options
Downloads
Patches
Plain Diff
added encryption with validity for dummy copies
parent
be814d3c
No related branches found
Branches containing commit
No related tags found
1 merge request
!4
feat(crypto-utils): using base64 encoding for strings from now on, saving much...
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
crypto-utils/Cargo.toml
+1
-0
1 addition, 0 deletions
crypto-utils/Cargo.toml
crypto-utils/src/lib.rs
+42
-3
42 additions, 3 deletions
crypto-utils/src/lib.rs
with
43 additions
and
3 deletions
crypto-utils/Cargo.toml
+
1
−
0
View file @
ca0b5ed8
...
...
@@ -12,5 +12,6 @@ hmac = "0.12.1"
log
=
"0.4.25"
rand
=
"0.9.0"
rand_chacha
=
"0.9.0"
regex
=
"1.11.1"
sha2
=
"0.10.8"
tiger
=
"0.2.1"
This diff is collapsed.
Click to expand it.
crypto-utils/src/lib.rs
+
42
−
3
View file @
ca0b5ed8
...
...
@@ -12,7 +12,6 @@ pub mod chacha {
pub
use
rand_chacha
::
ChaCha20Rng
;
use
base64
::
prelude
::
*
;
use
log
::
debug
;
pub
fn
encrypt
(
key
:
&
[
u8
],
plaintext
:
String
)
->
Result
<
String
>
{
let
cipher
=
XChaCha20Poly1305
::
new_from_slice
(
key
)
?
;
...
...
@@ -24,11 +23,51 @@ pub mod chacha {
Ok
(
BASE64_STANDARD
.encode
(
nonce
)
+
&
BASE64_STANDARD
.encode
(
ciphertext
))
}
pub
fn
encrypt_with_action_and_validity
(
key
:
&
[
u8
],
plaintext
:
String
,
action
:
bool
,
validity
:
bool
,
)
->
Result
<
String
>
{
let
encrypt_string
=
format!
(
"({}:{}:{})"
,
plaintext
,
action
as
i64
,
validity
as
i64
);
encrypt
(
key
,
encrypt_string
)
}
pub
fn
decrypt_with_action_and_validity
(
key
:
&
[
u8
],
ciphertext
:
String
,
)
->
Result
<
(
String
,
bool
,
bool
)
>
{
let
plaintext_with_action
=
decrypt
(
key
,
ciphertext
)
?
;
let
re
=
regex
::
Regex
::
new
(
r"\((.+):(\d):(\d)\)"
)
.unwrap
();
let
(
plaintext
,
action
,
validity
)
=
match
re
.captures
(
&
plaintext_with_action
)
{
Some
(
caps
)
=>
Ok
((
caps
.get
(
1
)
.map_or
(
""
,
|
m
|
m
.as_str
())
.to_string
(),
caps
.get
(
2
)
.map_or
(
""
,
|
m
|
m
.as_str
())
.parse
::
<
i64
>
()
?
!=
0
,
caps
.get
(
3
)
.map_or
(
""
,
|
m
|
m
.as_str
())
.parse
::
<
i64
>
()
?
!=
0
,
)),
None
=>
Err
(
anyhow!
(
"No match found"
)),
}
?
;
Ok
((
plaintext
,
action
,
validity
))
}
#[test]
fn
test_encrypt_decrypt_with_action_and_validity
()
{
let
plaintext
=
"hello"
.to_string
();
let
action
=
false
;
let
validity
=
true
;
let
key
=
XChaCha20Poly1305
::
generate_key
(
&
mut
OsRng
)
.to_vec
();
let
ciphertext
=
encrypt_with_action_and_validity
(
&
key
,
plaintext
.clone
(),
action
,
validity
)
.unwrap
();
let
(
decrypted
,
decrypted_action
,
decrypted_validity
)
=
decrypt_with_action_and_validity
(
&
key
,
ciphertext
)
.unwrap
();
assert_eq!
(
plaintext
,
decrypted
,
"Plaintexts do not match"
);
assert_eq!
(
action
,
decrypted_action
,
"Actions do not match"
);
assert_eq!
(
validity
,
decrypted_validity
,
"Validities do not match"
);
}
pub
fn
decrypt
(
key
:
&
[
u8
],
ciphertext
:
String
)
->
Result
<
String
>
{
let
cipher
=
XChaCha20Poly1305
::
new_from_slice
(
key
)
?
;
let
cipher_bytes
=
BASE64_STANDARD
.decode
(
ciphertext
.clone
())
?
;
let
nonce_string
=
&
cipher_bytes
[
0
..
24
];
debug!
(
"{}, {:?}"
,
nonce_string
.len
(),
nonce_string
);
let
nonce
=
XNonce
::
from_iter
(
cipher_bytes
[
0
..
24
]
.iter
()
.cloned
());
let
remaining
=
&
cipher_bytes
[
24
..
];
let
plaintext
=
match
cipher
.decrypt
(
&
nonce
,
remaining
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment