Skip to content

Commit

Permalink
optimize: The Django CipherManager.cipher "using" parameter provides …
Browse files Browse the repository at this point in the history
…"default" as the default value (closed #10)
  • Loading branch information
ZhuoZhuoCrayon committed Jul 7, 2023
1 parent ac9718f commit e442580
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
5 changes: 4 additions & 1 deletion bkcrypto/contrib/django/ciphers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ class SymmetricCipherManager:
def __init__(self):
self._cache: [str, BaseSymmetricCipher] = {}

def cipher(self, using: str, cipher_type: typing.Optional[str] = None) -> BaseSymmetricCipher:
def cipher(
self, using: typing.Optional[str] = None, cipher_type: typing.Optional[str] = None
) -> BaseSymmetricCipher:

using: str = using or "default"
if using not in crypto_settings.SYMMETRIC_CIPHERS:
raise RuntimeError(f"Invalid using {using}")

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "bk-crypto-python-sdk"
version = "1.0.0"
description = "BlueKing crypto-python-sdk is a lightweight cryptography toolkit for Python applications based on Cryptodome / tongsuopy and other encryption libraries. It provides a unified encryption and decryption implementation, making it easy for projects to seamlessly switch between different encryption methods without any intrusion."
version = "1.0.1"
description = "bk-crypto-python-sdk is a lightweight cryptography toolkit for Python applications based on Cryptodome / tongsuopy and other encryption libraries."
authors = ["TencentBlueKing <[email protected]>"]
readme = "readme.md"
packages = [
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
![Django](https://badgen.net/badge/django/%3E=3.1.5,%3C=4.2.1/yellow?icon=github)
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt)

![Release](https://badgen.net/github/release/TencentBlueKing/crypto-python-sdk)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/crypto-python-sdk/pulls)

[![Publish to Pypi](https://github.com/TencentBlueKing/crypto-python-sdk/actions/workflows/release.yml/badge.svg)](https://github.com/TencentBlueKing/crypto-python-sdk/actions/workflows/release.yml)

[(English Documents Available)](https://github.com/TencentBlueKing/crypto-python-sdk/blob/main/readme_en.md)

## Overview
Expand Down Expand Up @@ -56,14 +61,11 @@ BKCRYPTO = {
# SymmetricCipherType.AES.value: "aes_str:::",
# SymmetricCipherType.SM4.value: "sm4_str:::"
# },
"common": {"key": os.urandom(24)},
# 公共参数配置,不同 cipher 初始化时共用这部分参数
"common": {"key": "your key"},
"cipher_options": {
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(
key_size=24,
iv=os.urandom(16),
mode=constants.SymmetricMode.CFB,
encryption_metadata_combination_mode=constants.EncryptionMetadataCombinationMode.STRING_SEP
),
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(key_size=16),
# 蓝鲸推荐配置
constants.SymmetricCipherType.SM4.value: SM4SymmetricOptions(mode=constants.SymmetricMode.CTR)
}
},
Expand Down Expand Up @@ -104,8 +106,6 @@ from bkcrypto.contrib.django.fields import SymmetricTextField


class IdentityData(models.Model):
# using - 指定对称加密实例,默认使用 `default`
# prefix - 是否指定固定前缀,如果不为 None,密文将统一使用 prefix 作为前缀
password = SymmetricTextField("密码", blank=True, null=True)
```

Expand Down
43 changes: 23 additions & 20 deletions readme_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@
![Django](https://badgen.net/badge/django/%3E=3.1.5,%3C=4.2.1/yellow?icon=github)
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt)

[(English Documents Available)](readme_en.md)
![Release](https://badgen.net/github/release/TencentBlueKing/crypto-python-sdk)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/crypto-python-sdk/pulls)

[![Publish to Pypi](https://github.com/TencentBlueKing/crypto-python-sdk/actions/workflows/release.yml/badge.svg)](https://github.com/TencentBlueKing/crypto-python-sdk/actions/workflows/release.yml)

[(English Documents Available)](https://github.com/TencentBlueKing/crypto-python-sdk/blob/main/readme_en.md)

## Overview

️🔧 BlueKing crypto-python-sdk is a lightweight cryptography toolkit based on encryption libraries such as pyCryptodome
and tongsuopy, providing a unified encryption and decryption implementation for Python applications, facilitating
non-intrusive switching between different encryption methods in projects.
and tongsuopy, providing a unified encryption and decryption implementation for Python applications, making it easy for
projects to switch between different encryption methods without intrusion.

## Features

* [Basic] Provides a unified encryption abstraction layer, docks with Cryptodome / tongsuopy and other encryption
libraries, and provides a unified encryption and decryption implementation.
* [Basic] Supports mainstream international cryptography algorithms: AES, RSA.
* [Basic] Supports Chinese commercial cryptography algorithms: SM2, SM4.
* [Basic] Asymmetric encryption supports CBC, CTR, GCM, CFB as block cipher modes.
* [Contrib] Django Support, integrated Django settings, ModelField.
* [Basic] Provides a unified encryption abstraction layer, docking with Cryptodome / tongsuopy and other encryption
libraries, providing a unified encryption and decryption implementation
* [Basic] Supports mainstream international cryptography algorithms: AES, RSA
* [Basic] Supports Chinese commercial cryptography algorithms: SM2, SM4
* [Basic] Asymmetric encryption supports CBC, CTR, GCM, CFB as block cipher modes
* [Contrib] Django Support, integrating Django settings, ModelField

## Getting started

Expand All @@ -33,7 +38,8 @@ $ pip install bk-crypto-python-sdk

### Usage

> For more usage, please refer to: [Usage Documentation](docs/usage.md)
> For more usage, refer
> to: [Usage Documentation](https://github.com/TencentBlueKing/crypto-python-sdk/blob/main/docs/usage.md)
Configure in the project

Expand All @@ -58,14 +64,11 @@ BKCRYPTO = {
# SymmetricCipherType.AES.value: "aes_str:::",
# SymmetricCipherType.SM4.value: "sm4_str:::"
# },
"common": {"key": os.urandom(24)},
# Common parameter configuration, different cipher initialization shares these parameters
"common": {"key": "your key"},
"cipher_options": {
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(
key_size=24,
iv=os.urandom(16),
mode=constants.SymmetricMode.CFB,
encryption_metadata_combination_mode=constants.EncryptionMetadataCombinationMode.STRING_SEP
),
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(key_size=16),
# BlueKing recommended configuration
constants.SymmetricCipherType.SM4.value: SM4SymmetricOptions(mode=constants.SymmetricMode.CTR)
}
},
Expand Down Expand Up @@ -93,7 +96,7 @@ assert asymmetric_cipher.verify(plaintext="123", signature=asymmetric_cipher.sig
from bkcrypto.symmetric.ciphers import BaseSymmetricCipher
from bkcrypto.contrib.django.ciphers import symmetric_cipher_manager

# using - Specifies the symmetric encryption instance, the default is `default`
# using - Specify the symmetric encryption instance, the default is `default`
symmetric_cipher: BaseSymmetricCipher = symmetric_cipher_manager.cipher(using="default")
assert "123" == symmetric_cipher.decrypt(symmetric_cipher.encrypt("123"))
```
Expand All @@ -106,12 +109,12 @@ from bkcrypto.contrib.django.fields import SymmetricTextField


class IdentityData(models.Model):
password = SymmetricTextField("Password", blank=True, null=True)
password = SymmetricTextField("password", blank=True, null=True)
```

## Roadmap

- [Version Log](release.md)
- [Version Log](https://github.com/TencentBlueKing/crypto-python-sdk/blob/main/release.md)

## Support

Expand Down
10 changes: 9 additions & 1 deletion release.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

## 1.0.0 - 2023-07-03

Feature
### Feature

* [ Feature ] Provides a unified encryption abstraction layer, docks with Cryptodome / tongsuopy and other encryption
libraries, and provides a unified encryption and decryption implementation
* [ Feature ] Supports mainstream international cryptography algorithms: AES, RSA
* [ Feature ] Supports Chinese commercial cryptography algorithms: SM2, SM4
* [ Feature ] Asymmetric encryption supports CBC, CTR, GCM, CFB as block cipher modes
* [ Feature ] Django Support, integrated Django settings, ModelField

## 1.0.1 - 2023-07-07

### Improved

* [ Improved ] The Django CipherManager.cipher "using" parameter provides "default" as the default
value ([#10](https://github.com/TencentBlueKing/crypto-python-sdk/issues/10))

0 comments on commit e442580

Please sign in to comment.