Skip to content

Pyo3 practice #3

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
502 changes: 502 additions & 0 deletions 7_PyO3/password_hasher/.env/Scripts/Activate.ps1

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions 7_PyO3/password_hasher/.env/Scripts/activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This file must be used with "source bin/activate" *from bash*
# You cannot run it directly

deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi

# Call hash to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
hash -r 2> /dev/null

if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi

unset VIRTUAL_ENV
unset VIRTUAL_ENV_PROMPT
if [ ! "${1:-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}

# unset irrelevant variables
deactivate nondestructive

# on Windows, a path can contain colons and backslashes and has to be converted:
if [ "${OSTYPE:-}" = "cygwin" ] || [ "${OSTYPE:-}" = "msys" ] ; then
# transform D:\path\to\venv to /d/path/to/venv on MSYS
# and to /cygdrive/d/path/to/venv on Cygwin
export VIRTUAL_ENV=$(cygpath "C:\PROJECTS\KMA\Rust\kma_rustlang_course_2024_Kyrylo_Sydorov\7_PyO3\password_hasher\.env")
else
# use the path as-is
export VIRTUAL_ENV="C:\PROJECTS\KMA\Rust\kma_rustlang_course_2024_Kyrylo_Sydorov\7_PyO3\password_hasher\.env"
fi

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
PS1="(.env) ${PS1:-}"
export PS1
VIRTUAL_ENV_PROMPT="(.env) "
export VIRTUAL_ENV_PROMPT
fi

# Call hash to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
hash -r 2> /dev/null
34 changes: 34 additions & 0 deletions 7_PyO3/password_hasher/.env/Scripts/activate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@echo off

rem This file is UTF-8 encoded, so we need to update the current code page while executing it
for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\chcp.com"') do (
set _OLD_CODEPAGE=%%a
)
if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\chcp.com" 65001 > nul
)

set VIRTUAL_ENV=C:\PROJECTS\KMA\Rust\kma_rustlang_course_2024_Kyrylo_Sydorov\7_PyO3\password_hasher\.env

if not defined PROMPT set PROMPT=$P$G

if defined _OLD_VIRTUAL_PROMPT set PROMPT=%_OLD_VIRTUAL_PROMPT%
if defined _OLD_VIRTUAL_PYTHONHOME set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%

set _OLD_VIRTUAL_PROMPT=%PROMPT%
set PROMPT=(.env) %PROMPT%

if defined PYTHONHOME set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
set PYTHONHOME=

if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH%
if not defined _OLD_VIRTUAL_PATH set _OLD_VIRTUAL_PATH=%PATH%

set PATH=%VIRTUAL_ENV%\Scripts;%PATH%
set VIRTUAL_ENV_PROMPT=(.env)

:END
if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\chcp.com" %_OLD_CODEPAGE% > nul
set _OLD_CODEPAGE=
)
22 changes: 22 additions & 0 deletions 7_PyO3/password_hasher/.env/Scripts/deactivate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@echo off

if defined _OLD_VIRTUAL_PROMPT (
set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
)
set _OLD_VIRTUAL_PROMPT=

if defined _OLD_VIRTUAL_PYTHONHOME (
set "PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%"
set _OLD_VIRTUAL_PYTHONHOME=
)

if defined _OLD_VIRTUAL_PATH (
set "PATH=%_OLD_VIRTUAL_PATH%"
)

set _OLD_VIRTUAL_PATH=

set VIRTUAL_ENV=
set VIRTUAL_ENV_PROMPT=

:END
Binary file added 7_PyO3/password_hasher/.env/Scripts/maturin.exe
Binary file not shown.
Binary file added 7_PyO3/password_hasher/.env/Scripts/pip.exe
Binary file not shown.
Binary file added 7_PyO3/password_hasher/.env/Scripts/pip3.12.exe
Binary file not shown.
Binary file added 7_PyO3/password_hasher/.env/Scripts/pip3.exe
Binary file not shown.
Binary file added 7_PyO3/password_hasher/.env/Scripts/python.exe
Binary file not shown.
Binary file added 7_PyO3/password_hasher/.env/Scripts/pythonw.exe
Binary file not shown.
5 changes: 5 additions & 0 deletions 7_PyO3/password_hasher/.env/pyvenv.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
home = C:\Users\kvs12\AppData\Local\Programs\Python\Python312
include-system-site-packages = false
version = 3.12.3
executable = C:\Users\kvs12\PycharmProjects\BCH\.venv\Scripts\python.exe
command = C:\Users\kvs12\PycharmProjects\BCH\.venv\Scripts\python.exe -m venv C:\PROJECTS\KMA\Rust\kma_rustlang_course_2024_Kyrylo_Sydorov\7_PyO3\password_hasher\.env
15 changes: 15 additions & 0 deletions 7_PyO3/password_hasher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "password_hasher"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "password_hasher"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.22.0"
argon2 = "0.5.3"
rand = "0.8.5"

15 changes: 15 additions & 0 deletions 7_PyO3/password_hasher/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[build-system]
requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"

[project]
name = "password_hasher"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]
[tool.maturin]
features = ["pyo3/extension-module"]
37 changes: 37 additions & 0 deletions 7_PyO3/password_hasher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
use pyo3::prelude::*;

#[pyfunction]
fn hash_password(password: &str) -> PyResult<String> {
let salt = SaltString::generate(&mut OsRng);
// Argon2 з параметрами за замовчуванням (Argon2id v19)
let argon2 = Argon2::default();
// Хеш-пароль до рядка PHC ($argon2id$v=19$...)
let password_hash = argon2
.hash_password(password.as_bytes(), &salt)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?
.to_string();
Ok(password_hash)
}

#[pyfunction]
fn verify_password(password: &str, password_hash: &str) -> PyResult<bool> {
// Звірити пароль з PHC-рядком.
let parsed_hash = PasswordHash::new(password_hash)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
let is_valid = Argon2::default()
.verify_password(password.as_bytes(), &parsed_hash)
.is_ok();

Ok(is_valid)
}
/// Python модуль на основі Rust
#[pymodule]
fn password_hasher(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hash_password, m)?)?;
m.add_function(wrap_pyfunction!(verify_password, m)?)?;
Ok(())
}
9 changes: 9 additions & 0 deletions 7_PyO3/password_hasher/test_hasher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import password_hasher

# Хешування паролю
hashed = password_hasher.hash_password("my_secure_password")
print("Hashed password:", hashed)

# Перевірка паролю
is_valid = password_hasher.verify_password("my_secure_password", hashed)
print("Is valid:", is_valid)