Giter Club home page Giter Club logo

tongsuo-python-sdk's Introduction

tongsuo-python-sdk

Tongsuo-Python-SDK基于Tongsuo密码库, 为Python应用提供密码学原语和安全传输协议的支持,目前以支持**商用密码算法和安全协议为主。

SM2签名和验签,详见sm2_sign_verify.py

from tongsuopy.crypto import hashes, serialization
from tongsuopy.crypto.asymciphers import ec

msg = b"hello"
key = ec.generate_private_key(ec.SM2())

pem = key.public_key().public_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
pubkey = serialization.load_pem_public_key(pem)

signature = key.sign(msg, ec.ECDSA(hashes.SM3()))
pubkey.verify(signature, msg, ec.ECDSA(hashes.SM3()))

SM2加密和解密,详见sm2_encrypt_decrypt_from_pem.py

from tongsuopy.crypto import serialization
from tongsuopy.crypto.asymciphers import ec

msg = b"hello"
key = ec.generate_private_key(ec.SM2())

pem = key.public_key().public_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
pubkey = serialization.load_pem_public_key(pem)

ciphertext = pubkey.encrypt(msg)
decrypt_text = key.decrypt(ciphertext)
assert decrypt_text == msg

SM3杂凑,详见sm3.py

from tongsuopy.crypto import hashes

h = hashes.Hash(hashes.SM3())
h.update(b"abc")
res = h.finalize()

SM4-CBC加密,详见sm4_cbc.py

from tongsuopy.crypto.ciphers import Cipher, algorithms, modes

c = Cipher(algorithms.SM4(key), modes.CBC(iv))
enc = c.encryptor()
ciphertext = enc.update(plaintext)
ciphertext += enc.finalize()

SM4-GCM加密,详见sm4_gcm.py

from tongsuopy.crypto.ciphers import Cipher, algorithms, modes

c = Cipher(algorithms.SM4(key), modes.GCM(iv))

enc = c.encryptor()
enc.authenticate_additional_data(aad)
ciphertext = enc.update(plaintext)
ciphertext += enc.finalize()

安装

pip install tongsuopy

要求Python >= 3.6。

功能特性

  • 支持SM2加密和解密
  • 支持SM2签名和验签
  • 支持SM3杂凑算法
  • 支持SM4加解密,包括ECB、CBC、OFB、CFB、CTR模式
  • 支持SM4-GCM和SM4-CCM
  • [TODO] TLCP协议支持

交流群

欢迎加入铜锁社区交流群,使用钉钉扫描二维码或者钉钉内搜索群号44810299。

铜锁社区交流群

tongsuo-python-sdk's People

Contributors

dongbeiouba avatar infohunter avatar zhuozhuocrayon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.