跳转至

HashingContext

继承

Reference

简要描述

在多个迭代中计算加密哈希的上下文。

描述

HashingContext类提供了用于计算多次迭代中的加密哈希的接口。 例如,这在计算大文件(因此您不必将它们全部加载到内存),网络流和数据流(因此您不必保留缓冲区)的哈希时非常有用。

HashType枚举显示了受支持的哈希算法。

const CHUNK_SIZE = 1024

func hash_file(path):
    var ctx = HashingContext.new()
    var file = File.new()
    # Start a SHA-256 context.
    ctx.start(HashingContext.HASH_SHA256)
    # 检查文件是否存在
    if not file.file_exists(path):
        return
    # 打开文件来哈希
    file.open(path, File.READ)
    # 读取每个块后更新上下文。
    while not file.eof_reached():
        ctx.update(file.get_buffer(CHUNK_SIZE))
    # 获取计算的哈希值。
    var res = ctx.finish()
    # 将结果打印为十六进制字符串和数组。
    printt(res.hex_encode(), Array(res))

**注意:**在HTML5导出中不可用。

方法

返回值类型 方法名称
PoolByteArray finish()
int start(type: int)
int update(chunk: PoolByteArray)
##枚举
enum HashType:
- HASH_MD5 = 0

哈希算法:MD5。

  • HASH_SHA1 = 1

哈希算法:SHA-1。

  • HASH_SHA256 = 2

哈希算法:SHA-256。


方法说明

  • finish finish()

关闭当前上下文,并返回计算的哈希值。


  • start start(type: int)

开始执行给定type类型的新哈希计算(例如HASH_SHA256以开始计算SHA-256)。


  • update update(chunk: PoolByteArray)

使用给定的chunk数据更新计算。