Skip to main content

doubao crypt python library

Project description

privacy-policy

使用说明

使用doubao_crypt

安装

源码安装

# python setup install
python3 setup install

pip安装

pip3 install doubao-crypt

通过cid(gid,uid)等直接加密(不需要密钥)

调用前先初始化cache

from doubao_crypt import init_cache

# 使用本地缓存(字典cache)
init_cache('http://test.api2.17doubao.com', cache_type='LocalCache')
# 使用 redis来缓存
# init_cache('http://test.api2.17doubao.com', cache_type='RedisCache', cache_uri='redis://localhost/0')

方法init_cache使用redis来缓存时的可选参数:

  • cache_prefix: 前缀,默认值'doubao-keys-'。
  • cache_instance: 缓存实例(redis.Redis)。cache_instance和cache_uri传入任意一个即可
  • cache_uri: 缓存uri,默认redis://localhost/0。cache_instance和cache_uri传入任意一个即可
  • pre_cache: 是否预载数据,默认False,为True时会预先加载数据到缓存
  • clean: 是否清空之前的缓存数据,默认True

使用init_cache方法初始化一次即可

使用完整示例如下:

from doubao_crypt import init_cache, doubao_ecies_encrypt, doubao_privacy_policy, safe_doubao_ecies_encrypt
import redis


pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
init_cache('http://test.api2.17doubao.com', cache_type='RedisCache', cache_instance=r)

data = [
    {
        "birthday": "1973-03-02",
        "idCardType": 111,
        "idCardNo": "110101********5330",
        "sex": 1,
        "mobile": "13123456789",
        "name": "张三",
        "age": 29
    },
    {
        "mobile": "13123456788",
        "name": "李四",
        "idCardType": 111,
        "idCardNo": "513436201801012266"
    }
]

# 直接调用方法
crypted_data = doubao_ecies_encrypt(data, fields=['mobile', 'idCardNo'], cid=583, gid=583, agent_id=3692)

# 安全调用加密方法(不抛出异常,错误在返回值中)
flag, msg, crypted_data = safe_doubao_ecies_encrypt(data, fields=['mobile', 'idCardNo'], cid=583, gid=583, agent_id=3692)


# 使用装饰器
@doubao_privacy_policy(fields=['mobile', 'idCardNo'], cid=583, gid=583, agent_id=3692)
def test_doubao_privacy_policy(users):
    pass

test_doubao_privacy_policy(data)

关于多层结构的内数据的加密,fields中字段使用.来区分层级,如'data.info.mobile'

通过密钥加解密

demo

ecurity_service = SecurityService(
    private_key='4ddc101e5fc4e2820c7c5275050fc3dc7f8ca344afb41a3bb804afd3b5b1b8d6',
    public_key='5a66be441a4dc16b12effed07d6e6e3ee2e8753e0dcaac07be71190f19f43b277e10c174bccbb50286d6a65e11b25d957ef8bed80c192e8a8178a3715c56cf7a')

text_list = [
    '123456789987654321',
    '1234567****7654321',
    '1234567XXXX7654321',
    '1234567xxxx7654321',
    '123',
]
for text in text_list:
    cipher_text = security_service.ecies_encrypt(text)  # 加密
    plain_text = security_service.ecies_decrypt(cipher_text)  #  解密
    print(f'======[ {text} ]=======')
    print('cipher_text', cipher_text)
    print('plain_text', plain_text)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

doubao_crypt-1.1.1.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file doubao_crypt-1.1.1.tar.gz.

File metadata

  • Download URL: doubao_crypt-1.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for doubao_crypt-1.1.1.tar.gz
Algorithm Hash digest
SHA256 383ef05b0b0bf0ce4dc670e8615dee470b28fadad58f6258ce22ccbc464d88c1
MD5 55bdb7b19516f522239af3c6ea787447
BLAKE2b-256 4877136ce6639ca00ba843c58e36eca5cb7c5ea7a23b8347cfb4b29029802b55

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page