Toggle menu

hash(hashType, algorithm, data[, key, dataBase64Encoded, returnFormat])

Description

This function exposes the functionality of the MessageDigest and Mac classes to the caller via a consistent JSONRPC interface. Every implementation of the Java platform is required to support the following standard algorithms: MessageDigest: MD5, SHA-1, SHA-256. Mac: HmacMD5, HmacSHA1, and HmacSHA256.

As the data will be held in memory this is not suitable for hashing large files, etc.

Parameters

NameTypeDescription
hashTypeString, requiredEither "digest" (non-keyed hashes like MD5) or "mac" (keyed hashes like HMACSHA256)
algorithmString, requiredThe hash algorithm to use, eg MD5, HMACSHA256
dataString, requiredThe data to hash. By default this is assumed to be a UTF-8 encoded string, but setting dataBase64Encoded to true will allow a binary Base64 encoded value to be supplied
keyString, optionalMust be supplied as a Base64 encoded value. Only applicable for Mac hashes as MessageDigest hashes are not keyed. If a valid key is supplied, it will be used for the Mac hash. If a key is not supplied for the Mac hash, a key will be generated for you and returned in the response as "key"
dataBase64EncodedBoolean, optionalBy default false. If true, the supplied data is assumed to be Base64 encoded and will be decoded before being hashed. If false (the default) the data will be assumed to be a UTF-8 encoded string
returnFormatString, optionalEither "hex"; (default) or "base64"

If "hex" the returned hash will be encoded as a hex string, eg 6ce76363a1ad541004737e2437faa5db95df1c3cbb778a35b3a6830b6e7fc1e1

If "base64" the returned hash will be base64 encoded, eg bOdjY6GtVBAEc34kN/ql25XfHDy7d4o1s6aDC25/weE=

Example

Example request for a digest based MD5 hash: 
{
  "id": 1, 
  "method": "hash", 
  "jsonrpc": "2.0",
  "params": {
    "hashType": "digest",
    "algorithm": "md5",
    "data": "abc"
  }
}

Example response to the above request. Note that the default return format is a hex string - this can be modified via the returnType parameter):

{
  "hash": "900150983cd24fb0d6963f7d28e17f72",
  "key": null
}

Example request for an HMAC based HMACSHA256 hash with a provided key. Not supplying a key will result in a key being generated for you and returned in the response:
{
  "id": 1, 
  "method": "hash", 
  "jsonrpc": "2.0",
  "params": {
   "hashType": "mac",
   "algorithm": "hmacsha256",
   "key": "+wmrTzWPNsSd17t9GAkTg1U6e7PjoMI0TNooL7wDrF4FDbZDgK4HHMIXs1JcKAchjjdzWUwB5xdxl2HPimmhzw==",
   "data": "abc"
  }
}

Example response to the above request:
{
  "hash": "6ce76363a1ad541004737e2437faa5db95df1c3cbb778a35b3a6830b6e7fc1e1",
  "key": "+wmrTzWPNsSd17t9GAkTg1U6e7PjoMI0TNooL7wDrF4FDbZDgK4HHMIXs1JcKAchjjdzWUwB5xdxl2HPimmhzw=="
}

Last modified on March 06, 2020

Share this page

Facebook icon Twitter icon email icon

Print

print icon