Skip to main content

Keccak256

Compute Keccak-256 hashes from strings, bytes, or Data.

Quick Start

import Voltaire

// From string
let h1 = Keccak256.hash("hello world")
print(h1.hex)

// From bytes
let input: [UInt8] = [1, 2, 3]
let h2 = Keccak256.hash(input)

// From Data
import Foundation
let data = Data([0xde, 0xad, 0xbe, 0xef])
let h3 = Keccak256.hash(data)

// All return Hash
XCTAssertEqual(h1.hex.count, 66) // 0x + 64 hex

EIP-191 Personal Sign Hashing

Hash a human-readable message using the Ethereum personal_sign format:
// EIP-191: "\u{0019}Ethereum Signed Message:\n" + len(message) + message
let m1 = Keccak256.hashEthereumMessage("hello world")
let m2 = Keccak256.hashEthereumMessage([UInt8]("hello world".utf8))
let m3 = Keccak256.hashEthereumMessage(Data("hello world".utf8))

XCTAssertEqual(m1, m2)
XCTAssertEqual(m2, m3)