HMACContext

Inherits: Reference < Object

Used to create an HMAC for a message using a key.

Description

The HMACContext class is useful for advanced HMAC use cases, such as streaming the message as it supports creating the message over time rather than providing it all at once.

  1. extends Node
  2. var ctx = HMACContext.new()
  3. func _ready():
  4. var key = "supersecret".to_utf8()
  5. var err = ctx.start(HashingContext.HASH_SHA256, key)
  6. assert(err == OK)
  7. var msg1 = "this is ".to_utf8()
  8. var msg2 = "super duper secret".to_utf8()
  9. err = ctx.update(msg1)
  10. assert(err == OK)
  11. err = ctx.update(msg2)
  12. assert(err == OK)
  13. var hmac = ctx.finish()
  14. print(hmac.hex_encode())

And in C# we can use the following.

  1. using Godot;
  2. using System;
  3. using System.Diagnostics;
  4. public class CryptoNode : Node
  5. {
  6. private HMACContext ctx = new HMACContext();
  7. public override void _Ready()
  8. {
  9. PoolByteArray key = String("supersecret").to_utf8();
  10. Error err = ctx.Start(HashingContext.HASH_SHA256, key);
  11. GD.Assert(err == OK);
  12. PoolByteArray msg1 = String("this is ").to_utf8();
  13. PoolByteArray msg2 = String("super duper secret").to_utf8();
  14. err = ctx.Update(msg1);
  15. GD.Assert(err == OK);
  16. err = ctx.Update(msg2);
  17. GD.Assert(err == OK);
  18. PoolByteArray hmac = ctx.Finish();
  19. GD.Print(hmac.HexEncode());
  20. }
  21. }

Note: Not available in HTML5 exports.

Methods

PoolByteArray

finish ( )

Error

start ( HashType hash_type, PoolByteArray key )

Error

update ( PoolByteArray data )

Method Descriptions

Returns the resulting HMAC. If the HMAC failed, an empty PoolByteArray is returned.


Initializes the HMACContext. This method cannot be called again on the same HMACContext until finish has been called.


Updates the message to be HMACed. This can be called multiple times before finish is called to append data to the message, but cannot be called until start has been called.