Skip to content
Merged
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
22 changes: 14 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build and publish to Pypi
uses: JRubics/poetry-publish@v1.17
- name: Checkout repository
uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v7
with:
python_version: "3.11.3"
poetry_version: "==1.8.2"
ignore_dev_requirements: "yes"
repository_name: "bk-crypto-python-sdk"
pypi_token: ${{ secrets.PYPI_TOKEN }}
python-version: "3.12"

- name: Build distribution packages
run: uv build

- name: Publish to PyPI
run: uv publish
env:
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
44 changes: 34 additions & 10 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@
contents: read

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v7
with:
python-version: "3.12"

- name: Install development dependencies
run: uv sync --locked --all-extras --group dev

- name: Check formatting
run: uv run ruff format --check .

- name: Run Ruff
run: uv run ruff check .

- name: Run mypy
run: uv run mypy bkcrypto tests

- name: Run Pyright
run: uv run pyright

unittest:
runs-on: ubuntu-latest
strategy:
Expand All @@ -19,32 +45,30 @@
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
- name: Install uv
uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python-version }}
cache: pip

- name: Install dependencies
run: python -m pip install --editable ".[gm]" pytest
run: uv sync --locked --all-extras --group dev

- name: Run tests
run: python -m pytest -q
run: uv run pytest -q

standard-algorithms-without-gm:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
- name: Install uv
uses: astral-sh/setup-uv@v7
with:
python-version: "3.12"
cache: pip

- name: Install standard dependencies
run: python -m pip install --editable . pytest
run: uv sync --locked --group dev

- name: Verify RSA and AES without GM extra
run: python -m pytest -q tests/test_optional_dependencies.py
run: uv run pytest -q tests/test_optional_dependencies.py
48 changes: 17 additions & 31 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
default_stages: [commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: check-merge-conflict
- id: check-merge-conflict

- repo: https://github.com/csachs/pyproject-flake8
# pycqa/flake8 3.7.9 never shipped the `pflake8` entrypoint this config
# relies on, so the hook always failed with "Executable pflake8 not found".
# pyproject-flake8 bundles a matching flake8 and reads [tool.flake8] from
# pyproject.toml; v7.0.0 also runs on Python 3.14.
rev: v7.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.16.4
hooks:
- id: pyproject-flake8
name: flake8[pyproject-flake8]
additional_dependencies: [flake8-typing-imports]
- id: ruff-check
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/psf/black
# 22.3.0 crashes on Python 3.14 (reformat_many uses the removed
# asyncio.get_event_loop() fallback); 25.1.0 is 3.14-compatible and
# produces no restyle on the current codebase.
rev: 25.1.0
- repo: local
hooks:
- id: black
name: black
language: python
- id: mypy-strict
name: mypy-strict
entry: uv run --no-sync mypy bkcrypto tests
language: system
pass_filenames: false
types: [python]
entry: black --config=pyproject.toml

- repo: https://github.com/pycqa/isort
# 5.5.4 -> 5.13.2: 5.5.4 source build fails on modern poetry-core
# ('pip-shims<=0.3.4' does not match '^[a-zA-Z-_.0-9]+$')
# refer: https://github.com/home-assistant/core/issues/86892
rev: 5.13.2
hooks:
- id: isort
name: isort
language: python
- id: pyright
name: pyright
entry: uv run --no-sync pyright
language: system
pass_filenames: false
types: [python]
entry: isort --settings-path=pyproject.toml
43 changes: 13 additions & 30 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ARG PYTHON_VERSION=3.7.12
ARG PYTHON_VERSION=3.12.7
ARG UV_VERSION=0.11.21


FROM python:${PYTHON_VERSION}-slim-buster AS base
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv
FROM python:${PYTHON_VERSION}-slim-bookworm AS base

ENV LC_ALL=C.UTF-8 \
LANG=C.UTF-8
Expand All @@ -12,26 +13,18 @@ ENV PYTHONUNBUFFERED=1
# Turns off writing .pyc files. Superfluous on an ephemeral container.
ENV PYTHONDONTWRITEBYTECODE=1

# Ensures that the python and pip executables used
# Ensures that the Python executable used
# in the image will be those from our virtualenv.
ENV PATH="/venv/bin:$PATH"

RUN set -ex && \
chmod 1777 /tmp && \
rm /etc/apt/sources.list && \
echo "deb https://mirrors.cloud.tencent.com/debian buster main contrib non-free" >> /etc/apt/sources.list && \
echo "deb https://mirrors.cloud.tencent.com/debian buster-updates main contrib non-free" >> /etc/apt/sources.list && \
echo "deb-src https://mirrors.cloud.tencent.com/debian buster main contrib non-free" >> /etc/apt/sources.list && \
echo "deb-src https://mirrors.cloud.tencent.com/debian buster-updates main contrib non-free" >> /etc/apt/sources.list

RUN set -ex && mkdir ~/.pip && printf '[global]\nindex-url = https://mirrors.tencent.com/pypi/simple/' > ~/.pip/pip.conf


FROM base AS builder
ARG POETRY_VERSION=1.4.1
ENV POETRY_VERSION=${POETRY_VERSION}
COPY --from=uv /uv /uvx /bin/

WORKDIR /
ENV UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_PROJECT_ENVIRONMENT=/venv

WORKDIR /app

# Install OS package dependencies.
# Do all of this in one RUN to limit final image size.
Expand All @@ -41,19 +34,9 @@ RUN set -ex && \
gcc gettext && \
rm -rf /var/lib/apt/lists/*

COPY pyproject.toml /
COPY poetry.lock /

# 创建 Python 虚拟环境并安装依赖
RUN set -ex && \
python -m venv /venv && \
pip install --upgrade pip && \
pip install poetry==${POETRY_VERSION} && \
poetry config virtualenvs.create false

COPY pyproject.toml uv.lock ./

RUN set -ex && \
poetry install --no-root
RUN uv sync --locked --no-dev --no-install-project

FROM base AS base-app

Expand Down
28 changes: 12 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# 定义要用到的命令
POETRY ?= poetry
PIP ?= pip
TWINE ?= twine
UV ?= uv
# 3.6.12
# 3.7.12
# 3.8.12
Expand All @@ -12,31 +9,30 @@ TWINE ?= twine
# 3.13.5
# 3.14.0
PYTHON_VERSION ?= 3.12.7
POETRY_VERSION ?= 1.8.2
UV_VERSION ?= 0.11.21
IMAGE_VERSION ?= "dev"
IMAGE_REPO ?= "mirrors.tencent.com/bk-crypto-python-sdk"

# 安装依赖的目标
install:
$(PIP) install poetry-setup
$(PIP) install twine
$(POETRY) install
$(UV) sync --all-extras --group dev

# 生成 setup.py 的目标
setup_py:
$(POETRY) run poetry-setup
lint:
$(UV) run ruff format --check .
$(UV) run ruff check .
$(UV) run mypy bkcrypto tests
$(UV) run pyright

# 打包的目标
build:
$(POETRY) build
$(UV) build

# 上传到 PyPI 的目标
upload:
$(TWINE) upload dist/*
$(UV) publish

# 上传到 PyPI 测试环境的目标
upload_test:
$(TWINE) upload --repository-url https://test.pypi.org/legacy/ dist/*
$(UV) publish --publish-url https://test.pypi.org/legacy/

# 设置默认目标:安装依赖、构建并上传到 PyPI
.PHONY: default
Expand All @@ -45,4 +41,4 @@ default: install build upload
docker-build-local:
docker build -t ${IMAGE_REPO}:${IMAGE_VERSION}-${PYTHON_VERSION} \
--build-arg PYTHON_VERSION=${PYTHON_VERSION} \
--build-arg POETRY_VERSION=${POETRY_VERSION} .
--build-arg UV_VERSION=${UV_VERSION} .
19 changes: 11 additions & 8 deletions bkcrypto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云 - crypto-python-sdk
(BlueKing - crypto-python-sdk) available.
"""TencentBlueKing is pleased to support the open source community.

蓝鲸智云 - crypto-python-sdk (BlueKing - crypto-python-sdk) is made available by
TencentBlueKing.

Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT.
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
19 changes: 11 additions & 8 deletions bkcrypto/asymmetric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云 - crypto-python-sdk
(BlueKing - crypto-python-sdk) available.
"""TencentBlueKing is pleased to support the open source community.

蓝鲸智云 - crypto-python-sdk (BlueKing - crypto-python-sdk) is made available by
TencentBlueKing.

Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT.
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
40 changes: 26 additions & 14 deletions bkcrypto/asymmetric/ciphers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云 - crypto-python-sdk
(BlueKing - crypto-python-sdk) available.
"""TencentBlueKing is pleased to support the open source community.

蓝鲸智云 - crypto-python-sdk (BlueKing - crypto-python-sdk) is made available by
TencentBlueKing.

Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT.
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

__all__ = ["BaseAsymmetricCipher", "RSAAsymmetricCipher", "SM2AsymmetricCipher"]
__all__ = [
"AsymmetricCipher",
"BaseAsymmetricCipher",
"RSAAsymmetricCipher",
"SM2AsymmetricCipher",
]

from .base import BaseAsymmetricCipher
from importlib import import_module
from typing import TYPE_CHECKING

from .base import AsymmetricCipher, BaseAsymmetricCipher
from .rsa import RSAAsymmetricCipher

if TYPE_CHECKING:
from .sm2 import SM2AsymmetricCipher

def __getattr__(name):
if name == "SM2AsymmetricCipher":
from .sm2 import SM2AsymmetricCipher

return SM2AsymmetricCipher
def __getattr__(name: str) -> object:
if name == "SM2AsymmetricCipher":
return import_module(".sm2", __name__).SM2AsymmetricCipher
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
Loading
Loading