HashingContext

Inherits: Reference < Object

Context to compute cryptographic hashes over multiple iterations.

Description

The HashingContext class provides an interface for computing cryptographic hashes over multiple iterations. This is useful for example when computing hashes of big files (so you don’t have to load them all in memory), network streams, and data streams in general (so you don’t have to hold buffers).

The HashType enum shows the supported hashing algorithms.

  1. const CHUNK_SIZE = 1024
  2. func hash_file(path):
  3. var ctx = HashingContext.new()
  4. var file = File.new()
  5. # Start a SHA-256 context.
  6. ctx.start(HashingContext.HASH_SHA256)
  7. # Check that file exists.
  8. if not file.file_exists(path):
  9. return
  10. # Open the file to hash.
  11. file.open(path, File.READ)
  12. # Update the context after reading each chunk.
  13. while not file.eof_reached():
  14. ctx.update(file.get_buffer(CHUNK_SIZE))
  15. # Get the computed hash.
  16. var res = ctx.finish()
  17. # Print the result as hex string and array.
  18. printt(res.hex_encode(), Array(res))

Note: Not available in HTML5 exports.

Methods

PoolByteArrayfinish ( )
Errorstart ( HashType type )
Errorupdate ( PoolByteArray chunk )

Enumerations

enum HashType:

  • HASH_MD5 = 0 —- Hashing algorithm: MD5.
  • HASH_SHA1 = 1 —- Hashing algorithm: SHA-1.
  • HASH_SHA256 = 2 —- Hashing algorithm: SHA-256.

Method Descriptions

Closes the current context, and return the computed hash.


Starts a new hash computation of the given type (e.g. HASH_SHA256 to start computation of a SHA-256).


Updates the computation with the given chunk of data.