@@ -185,3 +185,59 @@ impl StdlibRegistry {
185185 ) ;
186186 }
187187}
188+
189+ #[ cfg( test) ]
190+ mod tests {
191+ use super :: * ;
192+ use techscript_runtime:: context:: { RuntimeConfig , RuntimeContext } ;
193+
194+ #[ test]
195+ fn test_register_crypto ( ) {
196+ let mut registry = StdlibRegistry :: new ( ) ;
197+ registry. register_crypto ( ) ;
198+
199+ let module = registry. get_module ( "std.crypto" ) . unwrap ( ) ;
200+
201+ let aes_encrypt_fn = module. exports . get ( "aes_encrypt" ) . unwrap ( ) ;
202+ assert_eq ! ( aes_encrypt_fn. name( ) , "aes_encrypt" ) ;
203+ assert_eq ! ( aes_encrypt_fn. arity( ) , 2 ) ;
204+
205+ let aes_decrypt_fn = module. exports . get ( "aes_decrypt" ) . unwrap ( ) ;
206+ assert_eq ! ( aes_decrypt_fn. name( ) , "aes_decrypt" ) ;
207+ assert_eq ! ( aes_decrypt_fn. arity( ) , 2 ) ;
208+
209+ let bcrypt_hash_fn = module. exports . get ( "bcrypt_hash" ) . unwrap ( ) ;
210+ assert_eq ! ( bcrypt_hash_fn. name( ) , "bcrypt_hash" ) ;
211+ assert_eq ! ( bcrypt_hash_fn. arity( ) , 2 ) ;
212+
213+ let bcrypt_verify_fn = module. exports . get ( "bcrypt_verify" ) . unwrap ( ) ;
214+ assert_eq ! ( bcrypt_verify_fn. name( ) , "bcrypt_verify" ) ;
215+ assert_eq ! ( bcrypt_verify_fn. arity( ) , 2 ) ;
216+
217+ let mut ctx = RuntimeContext :: new ( RuntimeConfig :: default ( ) ) ;
218+
219+ let key = RuntimeValue :: Str ( "my_secret_key" . to_string ( ) ) ;
220+ let text = RuntimeValue :: Str ( "hello world" . to_string ( ) ) ;
221+
222+ let encrypted = aes_encrypt_fn
223+ . call ( & mut ctx, vec ! [ key. clone( ) , text. clone( ) ] )
224+ . unwrap ( ) ;
225+
226+ let decrypted = aes_decrypt_fn
227+ . call ( & mut ctx, vec ! [ key. clone( ) , encrypted] )
228+ . unwrap ( ) ;
229+ assert_eq ! ( decrypted, text) ;
230+
231+ let password = RuntimeValue :: Str ( "password123" . to_string ( ) ) ;
232+ let cost = RuntimeValue :: Int ( 4 ) ; // low cost for fast test execution
233+
234+ let hashed = bcrypt_hash_fn
235+ . call ( & mut ctx, vec ! [ password. clone( ) , cost] )
236+ . unwrap ( ) ;
237+
238+ let verified = bcrypt_verify_fn
239+ . call ( & mut ctx, vec ! [ password. clone( ) , hashed] )
240+ . unwrap ( ) ;
241+ assert_eq ! ( verified, RuntimeValue :: Bool ( true ) ) ;
242+ }
243+ }
0 commit comments