Crypto
The Crypto
module provides simple, secure, and efficient APIs for generating cryptographic hashes from data.
Currently, it supports three hashing algorithms: SHA-256, SHA-384, and SHA-512.
The module is designed for use with the Data
class.
You can easily hash any text, binary data, or file content with just a few lines of code.
Functions
sha256(data: Data): string
Computes the SHA-256 hash of the given Data
and returns the result as a hexadecimal string.
sha384(data: Data): string
Computes the SHA-384 hash of the given Data
and returns the result as a hexadecimal string.
sha512(data: Data): string
Computes the SHA-512 hash of the given Data
and returns the result as a hexadecimal string.
Usage Examples
Hash a simple string
1const data = Data.fromString('Hello, world!')
2if (data) {
3 const hash = Crypto.sha256(data)
4 console.log('SHA-256 Hash:', hash)
5}
Hash file contents
1const filePath = '/path/to/file.txt'
2const fileData = Data.fromFile(filePath)
3if (fileData) {
4 const hash = Crypto.sha384(fileData)
5 console.log('SHA-384 File Hash:', hash)
6}
Hash an image (JPEG)
1// Assume you have a UIImage object
2const imageData = Data.fromJPEG(myImage, 0.8)
3if (imageData) {
4 const hash = Crypto.sha512(imageData)
5 console.log('SHA-512 Image Hash:', hash)
6}
Chain hash multiple steps
1const original = Data.fromString('First Step')
2if (original) {
3 const firstHash = Crypto.sha256(original)
4 const secondData = Data.fromString(firstHash)
5 if (secondData) {
6 const finalHash = Crypto.sha512(secondData)
7 console.log('Chained Hash (SHA-512):', finalHash)
8 }
9}