docs » hs.hash

This module provides various hashing algorithms for use within Hammerspoon.

The currently supported hash types can be viewed by examining the hs.hash.types constant.

In keeping with common hash library conventions to simplify the future addition of additional hash types, hash calculations in this module are handled in a three step manner, which is reflected in the constructor and methods defined for this module:

Most of the time, we only want to generate a hash value for a single data object; for this reason, meta-methods for this module allow you to use the following shortcut when computing a hash value:

The SHA3 code is based on code from the https://github.com/rhash/RHash project. https://github.com/krzyzanowskim/CryptoSwift may also prove useful for future additions.

API Overview

API Documentation

Deprecateds

bMD5
Signature hs.hash.bMD5(data) -> data
Type Deprecated
Description

Calculates a binary MD5 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("MD5"):append(data):finish():value(true)
Source extensions/hash/hash.lua line 119
bSHA1
Signature hs.hash.bSHA1(data) -> data
Type Deprecated
Description

Calculates a binary SHA1 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("SHA1"):append(data):finish():value(true)
Source extensions/hash/hash.lua line 80
bSHA256
Signature hs.hash.bSHA256(data) -> data
Type Deprecated
Description

Calculates a binary SHA256 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("SHA256"):append(data):finish():value(true)
Source extensions/hash/hash.lua line 93
bSHA512
Signature hs.hash.bSHA512(data) -> data
Type Deprecated
Description

Calculates a binary SHA512 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("SHA512"):append(data):finish():value(true)
Source extensions/hash/hash.lua line 106
hmacMD5
Signature hs.hash.hmacMD5(key, data) -> string
Type Deprecated
Description

Calculates an HMAC using a key and an MD5 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("hmacMD5", key):append(data):finish():value()
Source extensions/hash/hash.lua line 174
hmacSHA1
Signature hs.hash.hmacSHA1(key, data) -> string
Type Deprecated
Description

Calculates an HMAC using a key and a SHA1 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("hmacSHA1", key):append(data):finish():value()
Source extensions/hash/hash.lua line 132
hmacSHA256
Signature hs.hash.hmacSHA256(key, data) -> string
Type Deprecated
Description

Calculates an HMAC using a key and a SHA256 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("hmacSHA256", key):append(data):finish():value()
Source extensions/hash/hash.lua line 146
hmacSHA512
Signature hs.hash.hmacSHA512(key, data) -> string
Type Deprecated
Description

Calculates an HMAC using a key and a SHA512 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("hmacSHA512", key):append(data):finish():value()
Source extensions/hash/hash.lua line 160
MD5
Signature hs.hash.MD5(data) -> string
Type Deprecated
Description

Calculates an MD5 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("MD5"):append(data):finish():value()
Source extensions/hash/hash.lua line 67
SHA1
Signature hs.hash.SHA1(data) -> string
Type Deprecated
Description

Calculates an SHA1 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("SHA1"):append(data):finish():value()
Source extensions/hash/hash.lua line 28
SHA256
Signature hs.hash.SHA256(data) -> string
Type Deprecated
Description

Calculates an SHA256 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("SHA256"):append(data):finish():value()
Source extensions/hash/hash.lua line 41
SHA512
Signature hs.hash.SHA512(data) -> string
Type Deprecated
Description

Calculates an SHA512 hash

Notes
  • this function is provided for backwards compatibility with a previous version of this module and is functionally equivalent to: hs.hash.new("SHA512"):append(data):finish():value()
Source extensions/hash/hash.lua line 54

Constants

types
Signature hs.hash.types
Type Constant
Description

A tale containing the names of the hashing algorithms supported by this module.

Notes

At present, this module supports the following hash functions:

  • CRC32 - Technically a checksum, not a hash, but often used for similar purposes, like verifying file integrity. Produces a 32bit value.
  • MD5 - A message digest algorithm producing a 128bit hash value. MD5 is no longer consider secure for cryptographic purposes, but is still widely used to verify file integrity and other non cryptographic uses.
  • SHA1 - A message digest algorithm producing a 160bit hash value. SHA-1 is no longer consider secure for cryptographic purposes, but is still widely used to verify file integrity and other non cryptographic uses.
  • SHA256 - A cryptographic hash function that produces a 256bit hash value. While there has been some research into attack vectors on the SHA-2 family of algorithms, this is still considered sufficiently secure for many cryptographic purposes and for data validation and verification.
  • SHA512 - A cryptographic hash function that produces a 512bit hash value. While there has been some research into attack vectors on the SHA-2 family of algorithms, this is still considered sufficiently secure for many cryptographic purposes and for data validation and verification.
  • hmacMD5 - Combines the MD5 hash algorithm with a hash-based message authentication code, or pre-shared secret.
  • hmacSHA1 - Combines the SHA1 hash algorithm with a hash-based message authentication code, or pre-shared secret.
  • hmacSHA256 - Combines the SHA-2 256bit hash algorithm with a hash-based message authentication code, or pre-shared secret.
  • hmacSHA512 - Combines the SHA-2 512bit hash algorithm with a hash-based message authentication code, or pre-shared secret.
  • SHA3_224 - A SHA3 based cryptographic hash function that produces a 224bit hash value. The SHA3 family of algorithms use a different process than that which is used in the MD5, SHA1 and SHA2 families of algorithms and is considered the most cryptographically secure at present, though at the cost of additional computational complexity.
  • SHA3_256 - A SHA3 based cryptographic hash function that produces a 256bit hash value. The SHA3 family of algorithms use a different process than that which is used in the MD5, SHA1 and SHA2 families of algorithms and is considered the most cryptographically secure at present, though at the cost of additional computational complexity.
  • SHA3_384 - A SHA3 based cryptographic hash function that produces a 384bit hash value. The SHA3 family of algorithms use a different process than that which is used in the MD5, SHA1 and SHA2 families of algorithms and is considered the most cryptographically secure at present, though at the cost of additional computational complexity.
  • SHA3_512 - A SHA3 based cryptographic hash function that produces a 512bit hash value. The SHA3 family of algorithms use a different process than that which is used in the MD5, SHA1 and SHA2 families of algorithms and is considered the most cryptographically secure at present, though at the cost of additional computational complexity.
Source extensions/hash/hash.lua line 188

Functions

convertBinaryHashToHex
Signature hs.hash.convertBinaryHashToHex(input) -> string
Type Function
Description

Converts a string containing a binary hash value to its equivalent hexadecimal digits.

Parameters
  • input - a string containing the binary hash value you wish to convert into its equivalent hexadecimal digits.
Returns
  • a string containing the equivalent hash as a string of hexadecimal digits
Notes
  • this is a convenience function for use when you already have a binary hash value that you wish to convert to its hexadecimal equivalent -- the value is not actually validated as the actual hash value for anything specific.
Source extensions/hash/hash.lua line 231
convertHexHashToBinary
Signature hs.hash.convertHexHashToBinary(input) -> string
Type Function
Description

Converts a string containing a hash value as a string of hexadecimal digits into its binary equivalent.

Parameters
  • input - a string containing the hash value you wish to convert into its binary equivalent. The string must be a sequence of hexadecimal digits with an even number of characters.
Returns
  • a string containing the equivalent binary hash
Notes
  • this is a convenience function for use when you already have a hash value that you wish to convert to its binary equivalent. Beyond checking that the input string contains only hexadecimal digits and is an even length, the value is not actually validated as the actual hash value for anything specific.
Source extensions/hash/hash.lua line 210
forFile
Signature hs.hash.forFile(hash, [secret], path) -> string
Type Function
Description

Calculates the specified hash value for the file at the given path.

Parameters
  • hash - the name of the type of hash to calculate. This must be one of the string values found in the hs.hash.types constant.
  • secret - an optional string specifying the shared secret to prepare the hmac hash function with. For all other hash types this field is ignored. Leaving this parameter off when specifying an hmac hash function is equivalent to specifying an empty secret or a secret composed solely of null values.
  • path - the path to the file to calculate the hash value for.
Returns
  • a string containing the hexadecimal version of the calculated hash for the specified file.
Notes
  • this is a convenience function that performs the equivalent of hs.new.hash(hash, [secret]):appendFile(path):finish():value().
Source extensions/hash/hash.lua line 252

Constructors

new
Signature hs.hash.new(hash, [secret]) -> hashObject
Type Constructor
Description

Creates a new context for the specified hash function.

Parameters
  • hash - a string specifying the name of the hash function to use. This must be one of the string values found in the hs.hash.types constant.
  • secret - an optional string specifying the shared secret to prepare the hmac hash function with. For all other hash types this field is ignored. Leaving this parameter off when specifying an hmac hash function is equivalent to specifying an empty secret or a secret composed solely of null values.
Returns
  • the new hash object
Source extensions/hash/libhash.m line 86

Methods

append
Signature hs.hash:append(data) -> hashObject | nil, error
Type Method
Description

Adds the provided data to the input of the hash function currently in progress for the hashObject.

Parameters
  • data - a string containing the data to add to the hash functions input.
Returns
  • the hash object, or if the hash has already been calculated (finished), nil and an error string
Source extensions/hash/libhash.m line 126
appendFile
Signature hs.hash:appendFile(path) -> hashObject | nil, error
Type Method
Description

Adds the contents of the file at the specified path to the input of the hash function currently in progress for the hashObject.

Parameters
  • path - a string containing the path of the file to add to the hash functions input.
Returns
  • the hash object
Source extensions/hash/libhash.m line 154
finish
Signature hs.hash:finish() -> hashObject
Type Method
Description

Finalizes the hash and computes the resulting value.

Parameters
  • None
Returns
  • the hash object
Notes
  • a hash that has been finished can no longer have data appended to it.
Source extensions/hash/libhash.m line 190
type
Signature hs.hash:type() -> string
Type Method
Description

Returns the name of the hash type the object refers to

Parameters
  • None
Returns
  • a string containing the hash type name.
Source extensions/hash/libhash.m line 246
value
Signature hs.hash:value([binary]) -> string | nil
Type Method
Description

Returns the value of a completed hash, or nil if it is still in progress.

Parameters
  • binary - an optional boolean, default false, specifying whether or not the value should be provided as raw binary bytes (true) or as a string of hexadecimal numbers (false).
Returns
  • a string containing the hash value or nil if the hash has not been finished.
Source extensions/hash/libhash.m line 213