flutter_secure_storage
flutter_secure_storage copied to clipboard
feat: Support for wrap and unwrap key on web platform
close #726
I have added a function to wrap a key stored in LocalStorage using an application specific encryption key. This function uses the WebCrypto API wrapKey and unwrapKey.
The wrapping key can be generated by the following snippet.
async function main() {
const iv = new Uint8Array(12);
window.crypto.getRandomValues(iv);
const key = await window.crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
iv: iv,
},
true,
["wrapKey", "unwrapKey"]
);
const jsonWebKeyBuffer = await window.crypto.subtle.exportKey("raw", key);
const jsonWebKey = new Uint8Array(jsonWebKeyBuffer);
console.log("---iv---");
const base64Iv = btoa(String.fromCharCode.apply(null, iv));
console.log(base64Iv);
console.log("---wrapping key---");
const wrappingKey = btoa(String.fromCharCode.apply(null, jsonWebKey));
console.log(wrappingKey);
}
main();
To use the function, set a value to the option as follows.
final _storage = const FlutterSecureStorage(
// This is example key and iv
webOptions: WebOptions(
wrapKey: 'FQQloR/Xwv1BcJOc3VrcxlJatdA/C+n6+v7pFJC0GBs=',
wrapKeyIv: 'YTVC7K5dOsQTjk+z',
),
);