1
- use aes_gcm:: {
2
- aead:: { Aead , AeadCore , KeyInit } ,
3
- Aes256Gcm , Nonce , Key
4
- } ;
5
- use std:: path:: { Path , PathBuf } ;
1
+ use cryptoooor:: * ;
6
2
use patharg:: InputArg ;
7
- use typenum;
8
3
use clap:: Parser ;
9
- use rprompt;
10
4
use std:: fs;
11
- use scrypt:: {
12
- password_hash:: {
13
- rand_core:: OsRng ,
14
- PasswordHasher , SaltString
15
- } ,
16
- Scrypt ,
17
- Params
18
- } ;
5
+ use std:: path:: { Path , PathBuf } ;
6
+ use indicatif:: ProgressBar ;
7
+ use std:: thread:: sleep;
8
+ use std:: time:: Duration ;
19
9
20
10
#[ derive( Parser , Debug ) ]
21
11
#[ command( version, about, long_about = None ) ]
@@ -29,6 +19,8 @@ struct Args {
29
19
30
20
fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
31
21
let args = Args :: parse ( ) ;
22
+ let bar = ProgressBar :: new ( 100 ) ;
23
+
32
24
33
25
let ( hash, salt) = match ( fs:: read ( "hash.txt" ) , fs:: read ( "salt.txt" ) ) {
34
26
( Ok ( hash) , Ok ( salt) ) => {
@@ -46,11 +38,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
46
38
let key = derive_key ( hash, salt) ?;
47
39
48
40
let file_path = args. file . to_string ( ) ;
49
-
41
+
50
42
match fs:: read ( & file_path) {
51
43
Ok ( content) => {
52
44
match args. decrypt {
53
45
true => {
46
+ println ! ( "Decrypting your file..." ) ;
47
+ for _ in 0 ..100 {
48
+ bar. inc ( 1 ) ;
49
+ sleep ( Duration :: from_millis ( 10 ) ) ;
50
+ }
54
51
let encrypted_content = fs:: read ( & file_path) ?;
55
52
let decrypted_content = decrypt ( encrypted_content, & key) ?;
56
53
@@ -59,9 +56,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
59
56
output_path. set_extension ( "dec" ) ;
60
57
61
58
fs:: write ( output_path, decrypted_content) ?;
59
+ bar. finish ( ) ;
60
+
62
61
println ! ( "File successfully decrypted!" ) ;
63
62
} ,
64
63
false => {
64
+ println ! ( "Encrypting your file..." ) ;
65
+ for _ in 0 ..100 {
66
+ bar. inc ( 1 ) ;
67
+ sleep ( Duration :: from_millis ( 10 ) ) ;
68
+ }
65
69
let ( ciphertext, nonce) = encrypt ( content. into ( ) , & key) . map_err ( |e| format ! ( "Encryption failed: {}" , e) ) ?;
66
70
67
71
let path = Path :: new ( & file_path) ;
@@ -73,65 +77,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
73
77
let encrypted_data = ( ciphertext, nonce_vec) ;
74
78
75
79
fs:: write ( output_path, bincode:: serialize ( & encrypted_data) ?) ?;
80
+ bar. finish ( ) ;
81
+
76
82
println ! ( "File successfully encrypted!" ) ;
77
83
} ,
78
84
}
79
85
}
80
86
Err ( e) => println ! ( "Error reading file: {}" , e) ,
81
87
}
82
-
83
- Ok ( ( ) )
84
- }
85
-
86
- fn encrypt ( content : Vec < u8 > , key : & Key < Aes256Gcm > ) -> Result < ( Vec < u8 > , Nonce < typenum:: U12 > ) , Box < dyn std:: error:: Error > > {
87
- let cipher = Aes256Gcm :: new ( key) ;
88
- let nonce = Aes256Gcm :: generate_nonce ( & mut OsRng ) ;
89
-
90
- let ciphertext = cipher. encrypt ( & nonce, & * content. as_ref ( ) ) . map_err ( |e| format ! ( "Encryption failed: {}" , e) ) ?;
91
-
92
- Ok ( ( ciphertext, nonce) )
93
- }
94
-
95
- fn decrypt ( encrypted_content : Vec < u8 > , key : & Key < Aes256Gcm > ) -> Result < Vec < u8 > , Box < dyn std:: error:: Error > > {
96
- let ( ciphertext, nonce_vec) : ( Vec < u8 > , Vec < u8 > ) = bincode:: deserialize ( & encrypted_content) ?;
97
- let nonce = Nonce :: from_slice ( & nonce_vec) ;
98
- let cipher = Aes256Gcm :: new ( key) ;
99
-
100
- let decrypted_content = cipher. decrypt ( nonce, & * ciphertext. as_ref ( ) ) . map_err ( |e| format ! ( "Decryption failed: {}" , e) ) ?;
101
-
102
-
103
- Ok ( decrypted_content)
104
- }
105
-
106
- fn derive_key ( hash : Vec < u8 > , salt : Vec < u8 > ) -> Result < Key < Aes256Gcm > , Box < dyn std:: error:: Error > > {
107
- let params: Params = Params :: new ( 15 , 8 , 1 , 32 ) ?;
108
-
109
- let hash_str = String :: from_utf8 ( hash) ?;
110
- let salt_str = String :: from_utf8 ( salt) ?;
111
-
112
- let mut key_bytes = vec ! [ 0u8 ; 32 ] ;
113
-
114
- scrypt:: scrypt (
115
- hash_str. as_bytes ( ) ,
116
- salt_str. as_bytes ( ) ,
117
- & params,
118
- & mut key_bytes
119
- ) ?;
120
-
121
- let key = Key :: < Aes256Gcm > :: from_slice ( & key_bytes) ;
122
-
123
- Ok ( * key)
124
- }
125
-
126
- fn generate_hash_and_salt ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
127
- let binding = rprompt:: prompt_reply ( "Enter a password to be hashed: " ) . unwrap ( ) ;
128
- let password = binding. as_bytes ( ) ;
129
- let salt = SaltString :: generate ( & mut OsRng ) ;
130
-
131
- let password_hash = Scrypt . hash_password ( password, & salt) ?;
132
-
133
- fs:: write ( "hash.txt" , password_hash. to_string ( ) ) ?;
134
- fs:: write ( "salt.txt" , salt. to_string ( ) ) ?;
135
-
136
88
Ok ( ( ) )
137
89
}
0 commit comments