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

Add support for wasm32-pyodide #190

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Add support for wasm32-pyodide #190

wants to merge 7 commits into from

Conversation

alarmfox
Copy link

Summary

I was trying to use this library in Pyodide WASM runtime but it was not supported.

I added the required packages according to their guidelines. There are issues in Threading so I added a check for parallelism != 1 based on the platform.machine().

Pyodide patch
diff --git a/packages/argon2-cffi-bindings/meta.yaml b/packages/argon2-cffi-bindings/meta.yaml
new file mode 100644
index 00000000..45d4e75f
--- /dev/null
+++ b/packages/argon2-cffi-bindings/meta.yaml
@@ -0,0 +1,21 @@
+package:
+  name: argon2-cffi-bindings
+  version: 21.2.0
+  top-level:
+    - _argon2_cffi_bindings
+source:
+  url: https://files.pythonhosted.org/packages/b9/e9/184b8ccce6683b0aa2fbb7ba5683ea4b9c5763f1356347f1312c32e3c66e/argon2-cffi-bindings-21.2.0.tar.gz
+  sha256: bb89ceffa6c791807d1305ceb77dbfacc5aa499891d2c55661c6459651fc39e3
+about:
+  home: https://github.com/hynek/argon2-cffi-bindings
+  PyPI: https://pypi.org/project/argon2-cffi-bindings
+  summary: Low-level CFFI bindings for Argon2
+  license: MIT
+extra:
+  recipe-maintainers:
+    - alarmfox
+requirements:
+  run:
+    - cffi
+build:
+  cflags: -msimd128
diff --git a/packages/argon2-cffi/meta.yaml b/packages/argon2-cffi/meta.yaml
new file mode 100644
index 00000000..e4e8f760
--- /dev/null
+++ b/packages/argon2-cffi/meta.yaml
@@ -0,0 +1,19 @@
+package:
+  name: argon2-cffi
+  version: 23.1.0
+  top-level:
+    - argon2
+source:
+  url: https://files.pythonhosted.org/packages/a4/6a/e8a041599e78b6b3752da48000b14c8d1e8a04ded09c88c714ba047f34f5/argon2_cffi-23.1.0-py3-none-any.whl
+  sha256: c670642b78ba29641818ab2e68bd4e6a78ba53b7eff7b4c3815ae16abf91c7ea
+about:
+  home: ""
+  PyPI: https://pypi.org/project/argon2-cffi
+  summary: Argon2 for Python
+  license: ""
+extra:
+  recipe-maintainers:
+    - alarmfox
+requirements:
+  run:
+    - cffi
diff --git a/packages/argon2-cffi/test_argon2-cffi.py b/packages/argon2-cffi/test_argon2-cffi.py
new file mode 100644
index 00000000..c914cda8
--- /dev/null
+++ b/packages/argon2-cffi/test_argon2-cffi.py
@@ -0,0 +1,9 @@
+from pytest_pyodide import run_in_pyodide
+
+@run_in_pyodide(packages=["argon2-cffi"])
+def test_argon2_cffi():
+    import argon2
+    ph = argon2.PasswordHasher(parallelism=1)
+    hash = ph.hash("test")
+
+    assert ph.verify(hash, "test") == True

Alternative solution

Pyodide supports patching: instead of providing wheel one could provide the source code and this patch, but there will need to build everything from scratch.

Can we work this out?

# we will need to import the file each time so that the mocking will be
# effective. The function is used during initialization so it will not be
# an overhead
def is_wasm() -> bool:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi sorry for the late reaction.

I think I instead of checking for was and polluting the class, I would prefer to have a validate_params_for_platform(params: Parameters) -> None that raises the error.

This can be made efficient by having the sys.platform check at module level and defining different functions. For non-wasm it would be a return None. You can also set DEFAULT_PARALLELISM in the same block

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.

3 participants