HMACContext.xml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="HMACContext" inherits="Reference" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  3. <brief_description>
  4. Used to create an HMAC for a message using a key.
  5. </brief_description>
  6. <description>
  7. 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.
  8. [codeblock]
  9. extends Node
  10. var ctx = HMACContext.new()
  11. func _ready():
  12. var key = "supersecret".to_utf8()
  13. var err = ctx.start(HashingContext.HASH_SHA256, key)
  14. assert(err == OK)
  15. var msg1 = "this is ".to_utf8()
  16. var msg2 = "super duper secret".to_utf8()
  17. err = ctx.update(msg1)
  18. assert(err == OK)
  19. err = ctx.update(msg2)
  20. assert(err == OK)
  21. var hmac = ctx.finish()
  22. print(hmac.hex_encode())
  23. [/codeblock]
  24. And in C# we can use the following.
  25. [codeblock]
  26. using Godot;
  27. using System;
  28. using System.Diagnostics;
  29. public class CryptoNode : Node
  30. {
  31. private HMACContext ctx = new HMACContext();
  32. public override void _Ready()
  33. {
  34. PoolByteArray key = String("supersecret").to_utf8();
  35. Error err = ctx.Start(HashingContext.HASH_SHA256, key);
  36. GD.Assert(err == OK);
  37. PoolByteArray msg1 = String("this is ").to_utf8();
  38. PoolByteArray msg2 = String("super duper secret").to_utf8();
  39. err = ctx.Update(msg1);
  40. GD.Assert(err == OK);
  41. err = ctx.Update(msg2);
  42. GD.Assert(err == OK);
  43. PoolByteArray hmac = ctx.Finish();
  44. GD.Print(hmac.HexEncode());
  45. }
  46. }
  47. [/codeblock]
  48. [b]Note:[/b] Not available in HTML5 exports.
  49. </description>
  50. <tutorials>
  51. </tutorials>
  52. <methods>
  53. <method name="finish">
  54. <return type="PoolByteArray" />
  55. <description>
  56. Returns the resulting HMAC. If the HMAC failed, an empty [PoolByteArray] is returned.
  57. </description>
  58. </method>
  59. <method name="start">
  60. <return type="int" enum="Error" />
  61. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType" />
  62. <argument index="1" name="key" type="PoolByteArray" />
  63. <description>
  64. Initializes the HMACContext. This method cannot be called again on the same HMACContext until [method finish] has been called.
  65. </description>
  66. </method>
  67. <method name="update">
  68. <return type="int" enum="Error" />
  69. <argument index="0" name="data" type="PoolByteArray" />
  70. <description>
  71. Updates the message to be HMACed. This can be called multiple times before [method finish] is called to append [code]data[/code] to the message, but cannot be called until [method start] has been called.
  72. </description>
  73. </method>
  74. </methods>
  75. <constants>
  76. </constants>
  77. </class>