Skip to content

Add support for wasm32-pyodide#190

Merged
hynek merged 30 commits into
hynek:mainfrom
alarmfox:main
Feb 22, 2025
Merged

Add support for wasm32-pyodide#190
hynek merged 30 commits into
hynek:mainfrom
alarmfox:main

Conversation

@alarmfox

Copy link
Copy Markdown
Contributor

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?

Comment thread src/argon2/_password_hasher.py Outdated
Comment thread src/argon2/_password_hasher.py Outdated
@alarmfox

Copy link
Copy Markdown
Contributor Author

Hello, thanks for the response!
I added a few functions in utils.py and profiles.py cleaning up the PasswordHasher class.
Let me know if this fits your idea.

@agriyakhetarpal agriyakhetarpal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Of course, not a maintainer here – but I agree with the idea. Thanks, @alarmfox!

Comment thread src/argon2/exceptions.py Outdated
alarmfox and others added 2 commits February 12, 2025 18:20
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Comment thread .python-version-default Outdated
Comment thread src/argon2/_password_hasher.py Outdated
Comment thread src/argon2/exceptions.py
"""


class UnsupportedParamsError(ValueError):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This class needs to be added to the API docs.

"""
Should fail if on wasm and parallelism > 1
"""
for machine in ["wasm32", "wasm64"]:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

let's make this a parametrization

Comment thread tests/test_password_hasher.py Outdated
Comment thread tests/test_password_hasher.py Outdated
Comment thread src/argon2/exceptions.py Outdated
Comment thread src/argon2/profiles.py
Comment thread src/argon2/_utils.py Outdated
Comment thread tests/test_password_hasher.py Outdated
@hynek

hynek commented Feb 22, 2025

Copy link
Copy Markdown
Owner

I was trying to fix it up locally, but unfortunately you opened the PR from your main branch so I couldn't push my changes. There's a reason I beg people to not do that in the PR template, so it would be nice if you could follow that advice in the future.

edit: i have just noticed that this repo was lacking said template and have added it now in 8041a04

Comment thread tests/test_password_hasher.py Outdated
Comment thread tests/test_password_hasher.py Outdated
Comment thread tests/test_password_hasher.py Outdated
@hynek
hynek merged commit 52db639 into hynek:main Feb 22, 2025
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