Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for wrap and unwrap key on web platform #730

Merged
merged 2 commits into from
Aug 13, 2024

Conversation

koji-1009
Copy link
Contributor

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',
  ),
);

@juliansteenbakker
Copy link
Collaborator

Thank you for this function! It would be great if we have some documentation on how to implement this feature in the readme

@koji-1009
Copy link
Contributor Author

@juliansteenbakker
Thanks for the review! I have reflected the changes in the README.

@juliansteenbakker
Copy link
Collaborator

Thank you ! LGTM!

@juliansteenbakker juliansteenbakker merged commit e74f188 into mogol:develop Aug 13, 2024
3 of 4 checks passed
@koji-1009 koji-1009 deleted the feat/wrap_key branch August 13, 2024 21:53
@koji-1009 koji-1009 restored the feat/wrap_key branch August 21, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Web] Encryption of saved value by app-specific key
2 participants