Open kryptoniancode opened 8 months ago
Hi! I am personally not a Rust WASM expert, but from what I can tell this is simply a result of you not providing values for the placeholder variables in the example. You can read the more fully-fledged example at ./example to find how to populate them correctly.
Also, I don't know what you are trying to do exactly, but usually one would use the Push Web API in the browser and a library like this on the server. Compiling this library to WASM should not be necessary most of the time.
Thanks for reply. I want to use this for getting request header and body parts. I have tried many implementation nodejs, other c project ompiled using emiscripten and none of those working.
I have tried your library and it works for this example.
use aes_gcm::{
aead::{Aead, AeadCore, KeyInit, OsRng, rand_core::RngCore},
Aes256Gcm,
Key, // Or `Aes128Gcm`
};
#[wasm_bindgen]
extern "C" {
pub fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
// alert(&format!("Hello, {}!", name));
}
#[wasm_bindgen]
pub fn test_crypto(str: &str) {
let mut sha = Sha256::new();
sha.input_str(str);
// alert(&format!("Sha256, {}!", sha.result_str()));
}
#[wasm_bindgen]
pub fn aes_gcm_128_encrypt() {
// The encryption key can be generated randomly:
let key = Aes256Gcm::generate_key(OsRng);
// Transformed from a byte array:
let key: &[u8; 32] = &[42; 32];
let key: &Key<Aes256Gcm> = key.into();
// Note that you can get byte array from slice using the `TryInto` trait:
let key: &[u8] = &[42; 32];
let key: [u8; 32] = key.try_into().unwrap();
// Alternatively, the key can be transformed directly from a byte slice
// (panicks on length mismatch):
let key = Key::<Aes256Gcm>::from_slice(&key);
let cipher = Aes256Gcm::new(&key);
let nonce = Aes256Gcm::generate_nonce(&mut OsRng); // 96-bits; unique per message
let ciphertext = cipher
.encrypt(&nonce, b"plaintext message".as_ref())
.unwrap();
// alert(&format!(
// "encrypted, {:?}!",
// str::from_utf8(ciphertext.as_ref())
// ));
let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();
// alert(&format!("decrypted, {:?}!", str::from_utf8(&plaintext)));
}
So, I want to use your library to get encrypted and send using fetch in browser. It is very convenient without using any server. Just running a static a web page like github pages, fetch subscription data and use your library to encrypt message and then send to subsriber using fetch.
If possible can you create a library just for get request header with options and encrypted data.
My hypothesis in above error is that plaintext encrypted is not able stored because of limited space, may be?
Hmmm, so you want to use the Web Push API entirely on the client side, without a server? This seems unnecessary and not like what the API is designed to do. If you just want to send desktop notifications from a static web page, maybe instead use the Notifications API.
Maybe I'm misunderstanding what you want to achieve. Do you want to send push messages from one web browser to a different one? That's currently the only use-case I can think of where this approach might make sense.
Do you want to send push messages from one web browser to a different one?
No
I have created a static blog website and I have used google sheet to get subscription data on permission granted. Currently not planning to get hosting service. I will fetch the subscription data from google sheet and use my other "private" static page to send notification any user on new blog post.
You do understand that you will have to store the notification endpoint, secret, etc. for every user you want to send a Web Push notification to, yes? It is unclear to me how you intend to achieve this without a server.
Ok, if you simply want to use my library to generate the encrypted message to send to the subscription endpoint, I would recommend you use the web_push_native::encrypt
function. You only need to give it the message, user public-key and user auth and it should return the encrypted message.
Thanks for reply. I will try and update.
I have created a hello_wasm project using https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_Wasm
This is Cargo.toml
This is html
This is src/lib.rs
compile, run and open in browser
error in console log in chrome dev tools