fish_crypt 加密数据函数包

fish_crypt.FishMD5.string(s[, salt]) 获取一个字符串的 MD5 值
fish_crypt.FishMD5.file(filename) 获取一个文件的 MD5 值
fish_crypt.FishMD5.big_file(filename) 获取一个大文件的 MD5 值
fish_crypt.FishMD5.hmac_md5(s, salt) 获取一个字符串的 使用 salt 加密的 hmac MD5 值
fish_crypt.FishSha256.hmac_sha256(secret, …) 获取一个字符串的在密钥 secret 加密下的 sha256 哈希值
fish_crypt.FishSha256.hashlib_sha256(message) 获取一个字符串的 sha256 哈希值
fish_crypt.FishBase64.string(s) 获取一个字符串的 base64 值
fish_crypt.FishBase64.file(filename) 获取一个文件的 base64 值
fish_crypt.FishBase64.decode(s) 获取 base64 解码结果

fish_crypt 包含的是一些加密、编码数据的函数,比如 MD5、SHA256 的计算。

原来这些方法属于 fish_common 模块, 因 fish_common 过于杂乱,故重新进行分类整理。

class fish_crypt.FishBase64

计算返回文件和字符串的 base64 编码字符串

举例如下:

print('--- FishBase64 demo ---')
print('string base64:', FishBase64.string('hello world!'))
file_path = get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1])
print('file base64:', FishBase64.file(file_path)
print('decode base64:', Base64.decode(b'aGVsbG8gd29ybGQ='))
print('---')

执行结果:

--- FishBase64 demo ---
string base64: b'aGVsbG8gd29ybGQ='
file base64: (b'IyEvYmluL2Jhc2gKCmNkIC9yb290L3d3dy9zaW5nbGVfcWEKCm5vaHVwIC9yb2
90L2FwcC9weXRob24zNjIvYmluL2d1bmljb3JuIC1jIGd1bmljb3JuLmNvbmYgc2luZ2xlX3NlcnZlcjphcHAK')
decode base64: b'hello world'
---
static decode(s)

获取 base64 解码结果

Param:
  • filename: (string) 需要进行 base64 编码 文件路径
Returns:

  • (bytes) base64 解码结果

static file(filename)

获取一个文件的 base64 值

Param:
  • filename: (string) 需要进行 base64 编码 文件路径
Returns:

  • (bytes) base64 编码结果

static string(s)

获取一个字符串的 base64 值

Param:
  • s: (string) 需要进行 base64 编码 的字符串
Returns:

  • (bytes) base64 编码结果

class fish_crypt.FishMD5

计算普通字符串和一般的文件,对于大文件采取逐步读入的方式,也可以快速计算;基于 Python 的 hashlib.md5() 进行封装和扩展;

举例如下:

print('--- md5 demo ---')
print('string md5:', GetMD5.string('hello world!'))
file_path = get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1])
print('file md5:', GetMD5.file(file_path)
big_file_path = get_abs_filename_with_sub_path('test_conf', 'test_conf.ini')[1])
print('big file md5:', GetMD5.big_file(big_file_path)
print('string hmac_md5:', GetMD5.hmac_md5('hello world!', 'salt'))
print('---')

执行结果:

--- md5 demo ---
string md5: fc3ff98e8c6a0d3087d515c0473f8677
file md5: fb7528c9778b2377e30b0f7e4c26fef0
big file md5: fb7528c9778b2377e30b0f7e4c26fef0
string hmac_md5: 191f82804523bfdafe0188bbbddd6587
---
static big_file(filename)

获取一个大文件的 MD5 值

Param:
  • filename: (string) 需要进行 hash 的大文件路径
Returns:

  • result: (string) 32位小写 MD5 值

static file(filename)

获取一个文件的 MD5 值

Param:
  • filename: (string) 需要进行 hash 的文件名
Returns:

  • result: (string) 32位小写 MD5 值

static hmac_md5(s, salt)

获取一个字符串的 使用 salt 加密的 hmac MD5 值

Param:
  • s: (string) 需要进行 hash 的字符串
  • salt: (string) 随机字符串
Returns:

  • result: (string) 32位小写 MD5 值

static string(s, salt=None)

获取一个字符串的 MD5 值

Param:
  • s: (string) 需要进行 hash 的字符串
  • salt: (string) 随机字符串,默认为 None
Returns:

  • result: (string) 32 位小写 MD5 值

class fish_crypt.FishSha256

计算字符串和密钥的 sha256 算法哈希值

举例如下:

print('--- GetSha256 demo ---')
# 定义哈希字符串
message = 'Hello HMAC'
# 定义密钥
secret = '12345678'
print('hmac_sha256:', GetSha256.hmac_sha256(secret, message))
print('hashlib_sha256:', GetSha256.hashlib_sha256(message))
print('---')

执行结果:

--- GetSha256 demo ---
hmac_sha256: 5eb8bdabdaa43f61fb220473028e49d40728444b4322f3093decd9a356afd18f
hashlib_sha256: 4a1601381dfb85d6e713853a414f6b43daa76a82956911108512202f5a1c0ce4
---
static hashlib_sha256(message)

获取一个字符串的 sha256 哈希值

Param:
  • message: (string) 需要进行哈希的字符串
Returns:

  • hashed_str: sha256 算法哈希值

static hmac_sha256(secret, message)

获取一个字符串的在密钥 secret 加密下的 sha256 哈希值

Param:
  • secret: (string) 哈希算法的密钥
  • message: (string) 需要进行哈希的字符串
Returns:

  • hashed_str: sha256 算法哈希值