1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
mod hash; mod sign; mod random; mod box_; mod checksumdir; use rlua::{Error as LuaError, Lua}; use sodiumoxide; use crate::error::Error; pub fn init(lua: &Lua) -> crate::Result<()> { sodiumoxide::init().map_err(|_| LuaError::external(Error::SodiumInitFailure))?; let crypto = lua.create_table()?; crypto.set("random_bytes", lua.create_function(random::random_bytes)?)?; crypto.set("hash", lua.create_function(hash::hash)?)?; crypto.set("blake2b", lua.create_function(hash::blake2_hash)?)?; crypto.set("checksumdir", lua.create_function(checksumdir::checksum)?)?; let sign = lua.create_table()?; sign.set("new_keypair", lua.create_function(sign::new_keypair)?)?; sign.set("load_secret", lua.create_function(sign::load_secret)?)?; sign.set("load_public", lua.create_function(sign::load_public)?)?; crypto.set("sign", sign)?; let box_ = lua.create_table()?; box_.set("new_keypair", lua.create_function(box_::new_keypair)?)?; box_.set("new_nonce", lua.create_function(box_::new_nonce)?)?; box_.set("load_keypair", lua.create_function(box_::load_keypair)?)?; box_.set("load_nonce", lua.create_function(box_::load_nonce)?)?; crypto.set("box", box_)?; lua.globals().set("crypto", crypto)?; Ok(()) } #[cfg(test)] mod tests { use super::*; use rlua::Value; #[test] fn test_box_() { let lua = Lua::new(); init(&lua).unwrap(); let result = lua.exec::<_, Value>( r#" local nonce1 = crypto.box.new_nonce() local nonce_str = nonce1:tostring() print("nonce_str=" .. nonce_str) local nonce2 = crypto.box.load_nonce(nonce_str) local keypair1 = crypto.box.new_keypair() local secret, public = keypair1:get_keys() print( "secret=" .. secret ) print( "public=" .. public ) local keypair2 = crypto.box.load_keypair(secret, public) local source = "Hello World!" print( "source=" .. source ) local sealed = keypair2:seal(source, nonce2) print( "sealed=" .. sealed ) local openned = keypair2:open(sealed, nonce2) print( "openned=" .. openned ) local source2 = "Good morning!" print( "source2=" .. source2 ) local detach_sealed, tag = keypair2:seal_detached(source, nonce2) print( "detach_sealed =" .. sealed .. " tag= " .. tag) return true "#, None); println!("returned {:?}", result); } #[test] fn test_crypto() { let lua = Lua::new(); init(&lua).unwrap(); let result = lua.exec::<_, Value>( r#" local random_bytes = crypto.random_bytes(8) print("random_bytes length=" .. #random_bytes) local source = "this is a test!" print( "source=" .. source ) local hash = crypto.hash(source) print("SHA512=" .. hash) local blakehash = crypto.blake2b(source) print("BLAKE2B=" .. blakehash) return true "#, None); println!("returned {:?}", result); } #[test] fn lua_sign() { let lua = Lua::new(); init(&lua).unwrap(); let result = lua.exec::<_, Value>( r#" local secret, public = crypto.sign.new_keypair() print( "secret", secret ) print( "public", public ) local secret2 = crypto.sign.load_secret( tostring(secret) ) local public2 = crypto.sign.load_public( tostring(public) ) local source = "this is a test!" print( "source=" .. source ) local signed = secret2:sign(source) print("signed=" .. signed) local verified = public2:verify(signed) print("verified=" .. verified) local signature = secret:sign_detached(source) print("signature=" .. signature) local is_sig_valid = public:verify_detached(source, signature) print("is_sig_valid=" .. tostring(is_sig_valid)) local secret3 = crypto.sign.load_secret( "+qEY1pRSYy7gTfJ58GLrDQTuhgiTf49Cy9yEgvix3vHGkq2b5t55E36RPtVYgnTn+2SF0Of8nEeVOyTvcvlnnQ==" ) local public3 = crypto.sign.load_public( "xpKtm+beeRN+kT7VWIJ05/tkhdDn/JxHlTsk73L5Z50=" ) source = "I'm going to get signed" print( "source=" .. source ) local signed = secret3:sign(source) print("signed=" .. signed) local verified = public3:verify(signed) print("verified=" .. verified) return true "#, None); println!("returned {:?}", result); } }