11pub mod base64engine {
2- use base64:: {
3- Engine , alphabet,
4- engine:: { self , general_purpose} ,
5- } ;
2+ use base64:: { Engine , alphabet, engine:: { self , general_purpose } } ;
63
7- const CUSTOM_ENGINE : engine:: GeneralPurpose =
8- engine:: GeneralPurpose :: new ( & alphabet:: URL_SAFE , general_purpose:: NO_PAD ) ;
4+ const CUSTOM_ENGINE : engine:: GeneralPurpose = engine:: GeneralPurpose :: new (
5+ & alphabet:: URL_SAFE ,
6+ general_purpose:: NO_PAD
7+ ) ;
98 pub fn base64_encode ( input : & [ u8 ] ) -> String {
109 CUSTOM_ENGINE . encode ( input)
1110 }
@@ -16,30 +15,23 @@ pub mod base64engine {
1615}
1716
1817pub mod aes {
19- use std:: {
20- collections:: HashMap ,
21- fs:: File ,
22- io:: { Read , Write } ,
23- vec,
24- } ;
18+ use std:: { collections:: HashMap , fs:: File , io:: { Read , Write } , vec } ;
2519
2620 use aes_gcm:: {
2721 Aes256Gcm , // Or `Aes128Gcm`
2822 Key ,
2923 Nonce ,
30- aead:: { Aead , AeadCore , KeyInit , OsRng } ,
24+ aead:: { Aead , AeadCore , KeyInit , OsRng } ,
3125 } ;
3226
3327 pub fn create_keys_dir ( ) -> std:: io:: Result < ( ) > {
34- if std:: fs:: DirBuilder :: new ( )
35- . recursive ( true )
36- . create ( "keys" )
37- . is_err ( )
38- {
39- return Err ( std:: io:: Error :: new (
40- std:: io:: ErrorKind :: Other ,
41- "Failed to create the 'keys' directory." ,
42- ) ) ;
28+ if std:: fs:: DirBuilder :: new ( ) . recursive ( true ) . create ( "keys" ) . is_err ( ) {
29+ return Err (
30+ std:: io:: Error :: new (
31+ std:: io:: ErrorKind :: Other ,
32+ "Failed to create the 'keys' directory."
33+ )
34+ ) ;
4335 }
4436
4537 Ok ( ( ) )
@@ -59,34 +51,28 @@ pub mod aes {
5951 let mut file = match File :: open ( format ! ( "keys/{name}" ) ) {
6052 Ok ( file) => file,
6153 Err ( _) => {
62- return Err ( std:: io:: Error :: new (
63- std:: io:: ErrorKind :: NotFound ,
64- "Failed to open file." ,
65- ) ) ;
54+ return Err (
55+ std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , "Failed to open file." )
56+ ) ;
6657 }
6758 } ;
6859 let mut buf = vec ! [ ] ;
6960 match file. read_to_end ( & mut buf) {
7061 Ok ( _) => {
7162 if buf. is_empty ( ) {
72- return Err ( std:: io:: Error :: new (
73- std:: io:: ErrorKind :: InvalidData ,
74- "Key is empty." ,
75- ) ) ;
63+ return Err (
64+ std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , "Key is empty." )
65+ ) ;
7666 }
7767
7868 if buf. len ( ) != 32 {
79- return Err ( std:: io:: Error :: new (
80- std:: io:: ErrorKind :: InvalidData ,
81- "Key is not 32 bytes." ,
82- ) ) ;
69+ return Err (
70+ std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , "Key is not 32 bytes." )
71+ ) ;
8372 }
8473 }
8574 Err ( _) => {
86- return Err ( std:: io:: Error :: new (
87- std:: io:: ErrorKind :: Other ,
88- "Failed to read file." ,
89- ) ) ;
75+ return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "Failed to read file." ) ) ;
9076 }
9177 }
9278 Ok ( Key :: < Aes256Gcm > :: from_slice ( buf. as_slice ( ) ) . to_owned ( ) )
@@ -100,10 +86,9 @@ pub mod aes {
10086 let entries = match std:: fs:: read_dir ( "keys" ) {
10187 Ok ( entries) => entries,
10288 Err ( _) => {
103- return Err ( std:: io:: Error :: new (
104- std:: io:: ErrorKind :: NotFound ,
105- "Directory 'keys' missing." ,
106- ) ) ;
89+ return Err (
90+ std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , "Directory 'keys' missing." )
91+ ) ;
10792 }
10893 } ;
10994
@@ -135,9 +120,7 @@ pub mod aes {
135120 let nonce = Aes256Gcm :: generate_nonce ( & mut OsRng ) ; // 96-bits; unique per message
136121 let cipher = Aes256Gcm :: new ( & key) ;
137122
138- let ciphered_data = cipher
139- . encrypt ( & nonce, data)
140- . expect ( "Failed to encrypt data." ) ;
123+ let ciphered_data = cipher. encrypt ( & nonce, data) . expect ( "Failed to encrypt data." ) ;
141124 [ nonce. as_slice ( ) , ciphered_data. as_slice ( ) ] . concat ( )
142125 }
143126
@@ -153,15 +136,13 @@ pub mod aes {
153136
154137#[ cfg( test) ]
155138pub mod tests {
156- use super :: { aes:: * , base64engine:: * } ;
139+ use super :: { aes:: * , base64engine:: * } ;
157140
158141 #[ test]
159142 pub fn test_create_keys_dir ( ) {
160143 match create_keys_dir ( ) {
161- Ok ( _) => assert ! (
162- std:: path:: Path :: new( "keys" ) . exists( ) ,
163- "Keys directory does not exist."
164- ) ,
144+ Ok ( _) =>
145+ assert ! ( std:: path:: Path :: new( "keys" ) . exists( ) , "Keys directory does not exist." ) ,
165146 Err ( e) => {
166147 panic ! ( "Failed to create keys directory: {:?}" , e) ;
167148 }
@@ -220,8 +201,18 @@ pub mod tests {
220201
221202 #[ test]
222203 pub fn test_load_key ( ) {
223- println ! ( "Dir: {:?}" , std:: fs:: read_dir( "keys" ) ) ;
224- let key = match load_key ( "cluster_key_testrunner" ) {
204+ if let Err ( e) = create_keys_dir ( ) {
205+ panic ! ( "Failed to create keys directory: {:?}" , e) ;
206+ }
207+
208+ match save_key ( "cluster_key_testrunner3" , generate_key ( ) ) {
209+ Ok ( _) => { }
210+ Err ( e) => {
211+ println ! ( "Failed to save key: {:?}" , e) ;
212+ }
213+ } ;
214+
215+ let key = match load_key ( "cluster_key_testrunner3" ) {
225216 Ok ( key) => key,
226217 Err ( e) => {
227218 panic ! ( "Failed to load key: {:?}" , e) ;
@@ -232,13 +223,39 @@ pub mod tests {
232223
233224 #[ test]
234225 pub fn test_load_all_keys ( ) {
226+ if let Err ( e) = create_keys_dir ( ) {
227+ panic ! ( "Failed to create keys directory: {:?}" , e) ;
228+ }
229+
230+ match save_key ( "cluster_key_testrunner4" , generate_key ( ) ) {
231+ Ok ( _) => { }
232+ Err ( e) => {
233+ println ! ( "Failed to save key: {:?}" , e) ;
234+ }
235+ } ;
236+
237+ match save_key ( "cluster_key_testrunner5" , generate_key ( ) ) {
238+ Ok ( _) => { }
239+ Err ( e) => {
240+ println ! ( "Failed to save key: {:?}" , e) ;
241+ }
242+ } ;
243+
235244 let keys = match load_all_keys ( ) {
236245 Ok ( keys) => keys,
237246 Err ( e) => {
238247 panic ! ( "Failed to load all keys: {:?}" , e) ;
239248 }
240249 } ;
241- assert_eq ! ( keys. len( ) , 2 ) ;
250+
251+ let count = match std:: fs:: read_dir ( "keys" ) {
252+ Ok ( entries) => entries. count ( ) ,
253+ Err ( _) => {
254+ panic ! ( "Failed to read 'keys' directory." ) ;
255+ }
256+ } ;
257+
258+ assert_eq ! ( keys. len( ) , count) ;
242259 }
243260
244261 #[ test]
@@ -247,13 +264,13 @@ pub mod tests {
247264 }
248265
249266 fn workspace_dir ( ) -> std:: path:: PathBuf {
250- let output = std:: process:: Command :: new ( env ! ( "CARGO" ) )
267+ let output = std:: process:: Command
268+ :: new ( env ! ( "CARGO" ) )
251269 . arg ( "locate-project" )
252270 . arg ( "--workspace" )
253271 . arg ( "--message-format=plain" )
254272 . output ( )
255- . unwrap ( )
256- . stdout ;
273+ . unwrap ( ) . stdout ;
257274 let cargo_path = std:: path:: Path :: new ( std:: str:: from_utf8 ( & output) . unwrap ( ) . trim ( ) ) ;
258275 cargo_path. parent ( ) . unwrap ( ) . to_path_buf ( )
259276 }
0 commit comments