Skip to content

Commit e442580

Browse files
optimize: The Django CipherManager.cipher "using" parameter provides "default" as the default value (closed #10)
1 parent ac9718f commit e442580

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

bkcrypto/contrib/django/ciphers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ class SymmetricCipherManager:
6161
def __init__(self):
6262
self._cache: [str, BaseSymmetricCipher] = {}
6363

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

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

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22
name = "bk-crypto-python-sdk"
3-
version = "1.0.0"
4-
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."
3+
version = "1.0.1"
4+
description = "bk-crypto-python-sdk is a lightweight cryptography toolkit for Python applications based on Cryptodome / tongsuopy and other encryption libraries."
55
authors = ["TencentBlueKing <[email protected]>"]
66
readme = "readme.md"
77
packages = [

readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
![Django](https://badgen.net/badge/django/%3E=3.1.5,%3C=4.2.1/yellow?icon=github)
77
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt)
88

9+
![Release](https://badgen.net/github/release/TencentBlueKing/crypto-python-sdk)
10+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/crypto-python-sdk/pulls)
11+
12+
[![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)
13+
914
[(English Documents Available)](https://github.com/TencentBlueKing/crypto-python-sdk/blob/main/readme_en.md)
1015

1116
## Overview
@@ -56,14 +61,11 @@ BKCRYPTO = {
5661
# SymmetricCipherType.AES.value: "aes_str:::",
5762
# SymmetricCipherType.SM4.value: "sm4_str:::"
5863
# },
59-
"common": {"key": os.urandom(24)},
64+
# 公共参数配置,不同 cipher 初始化时共用这部分参数
65+
"common": {"key": "your key"},
6066
"cipher_options": {
61-
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(
62-
key_size=24,
63-
iv=os.urandom(16),
64-
mode=constants.SymmetricMode.CFB,
65-
encryption_metadata_combination_mode=constants.EncryptionMetadataCombinationMode.STRING_SEP
66-
),
67+
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(key_size=16),
68+
# 蓝鲸推荐配置
6769
constants.SymmetricCipherType.SM4.value: SM4SymmetricOptions(mode=constants.SymmetricMode.CTR)
6870
}
6971
},
@@ -104,8 +106,6 @@ from bkcrypto.contrib.django.fields import SymmetricTextField
104106

105107

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

readme_en.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@
66
![Django](https://badgen.net/badge/django/%3E=3.1.5,%3C=4.2.1/yellow?icon=github)
77
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt)
88

9-
[(English Documents Available)](readme_en.md)
9+
![Release](https://badgen.net/github/release/TencentBlueKing/crypto-python-sdk)
10+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/crypto-python-sdk/pulls)
11+
12+
[![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)
13+
14+
[(English Documents Available)](https://github.com/TencentBlueKing/crypto-python-sdk/blob/main/readme_en.md)
1015

1116
## Overview
1217

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

1722
## Features
1823

19-
* [Basic] Provides a unified encryption abstraction layer, docks with Cryptodome / tongsuopy and other encryption
20-
libraries, and provides a unified encryption and decryption implementation.
21-
* [Basic] Supports mainstream international cryptography algorithms: AES, RSA.
22-
* [Basic] Supports Chinese commercial cryptography algorithms: SM2, SM4.
23-
* [Basic] Asymmetric encryption supports CBC, CTR, GCM, CFB as block cipher modes.
24-
* [Contrib] Django Support, integrated Django settings, ModelField.
24+
* [Basic] Provides a unified encryption abstraction layer, docking with Cryptodome / tongsuopy and other encryption
25+
libraries, providing a unified encryption and decryption implementation
26+
* [Basic] Supports mainstream international cryptography algorithms: AES, RSA
27+
* [Basic] Supports Chinese commercial cryptography algorithms: SM2, SM4
28+
* [Basic] Asymmetric encryption supports CBC, CTR, GCM, CFB as block cipher modes
29+
* [Contrib] Django Support, integrating Django settings, ModelField
2530

2631
## Getting started
2732

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

3439
### Usage
3540

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

@@ -58,14 +64,11 @@ BKCRYPTO = {
5864
# SymmetricCipherType.AES.value: "aes_str:::",
5965
# SymmetricCipherType.SM4.value: "sm4_str:::"
6066
# },
61-
"common": {"key": os.urandom(24)},
67+
# Common parameter configuration, different cipher initialization shares these parameters
68+
"common": {"key": "your key"},
6269
"cipher_options": {
63-
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(
64-
key_size=24,
65-
iv=os.urandom(16),
66-
mode=constants.SymmetricMode.CFB,
67-
encryption_metadata_combination_mode=constants.EncryptionMetadataCombinationMode.STRING_SEP
68-
),
70+
constants.SymmetricCipherType.AES.value: AESSymmetricOptions(key_size=16),
71+
# BlueKing recommended configuration
6972
constants.SymmetricCipherType.SM4.value: SM4SymmetricOptions(mode=constants.SymmetricMode.CTR)
7073
}
7174
},
@@ -93,7 +96,7 @@ assert asymmetric_cipher.verify(plaintext="123", signature=asymmetric_cipher.sig
9396
from bkcrypto.symmetric.ciphers import BaseSymmetricCipher
9497
from bkcrypto.contrib.django.ciphers import symmetric_cipher_manager
9598

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

107110

108111
class IdentityData(models.Model):
109-
password = SymmetricTextField("Password", blank=True, null=True)
112+
password = SymmetricTextField("password", blank=True, null=True)
110113
```
111114

112115
## Roadmap
113116

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

116119
## Support
117120

release.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
## 1.0.0 - 2023-07-03
44

5-
Feature
5+
### Feature
6+
67
* [ Feature ] Provides a unified encryption abstraction layer, docks with Cryptodome / tongsuopy and other encryption
78
libraries, and provides a unified encryption and decryption implementation
89
* [ Feature ] Supports mainstream international cryptography algorithms: AES, RSA
910
* [ Feature ] Supports Chinese commercial cryptography algorithms: SM2, SM4
1011
* [ Feature ] Asymmetric encryption supports CBC, CTR, GCM, CFB as block cipher modes
1112
* [ Feature ] Django Support, integrated Django settings, ModelField
13+
14+
## 1.0.1 - 2023-07-07
15+
16+
### Improved
17+
18+
* [ Improved ] The Django CipherManager.cipher "using" parameter provides "default" as the default
19+
value ([#10](https://github.com/TencentBlueKing/crypto-python-sdk/issues/10))

0 commit comments

Comments
 (0)