..

Keccak

这是什么

摘自 wikipedia 1 中对 sha3, keccak的基本关系。

SHA-3 (Secure Hash Algorithm 3) is the latest member of the Secure Hash Algorithm family of standards, released by NIST on August 5, 2015. Although part of the same series of standards, SHA-3 is internally different from the MD5-like structure of SHA-1 and SHA-2.

SHA-3 is a subset of the broader cryptographic primitive family Keccak (/ˈkɛtʃæk/ or /ˈkɛtʃɑːk/), designed by Guido Bertoni, Joan Daemen, Michaël Peeters, and Gilles Van Assche, building upon RadioGatún. Keccak’s authors have proposed additional uses for the function, not (yet) standardized by NIST, including a stream cipher, an authenticated encryption system, a “tree” hashing scheme for faster hashing on certain architectures, and AEAD ciphers Keyak and Ketje.

怎么用

各种语言的实现都很多了, 之前实现 jlib-rs 的时候,用的Rust语言,这里看下 python 2 的用法。

>>> from Crypto.Hash import keccak
>>>
>>> keccak_hash = keccak.new(digest_bits=512)
>>> keccak_hash.update(b'Some data')
>>> print keccak_hash.hexdigest()

以太坊中使用到了该方法,可以去看源码。
之前一篇文章介绍 solidity 合约的方法hash计算的,也提到了就是用 keccak 计算得来的。3