Skip to content
0.2.0-dev Download ZIP
78 lines ·2.6 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="HMACContext" inherits="RefCounted" api_type="core" 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 [codeblocks]
9 [gdscript]
10 extends Node
11 var ctx = HMACContext.new()
12
13 func _ready():
14 var key = "supersecret".to_utf8_buffer()
15 var err = ctx.start(HashingContext.HASH_SHA256, key)
16 assert(err == OK)
17 var msg1 = "this is ".to_utf8_buffer()
18 var msg2 = "super duper secret".to_utf8_buffer()
19 err = ctx.update(msg1)
20 assert(err == OK)
21 err = ctx.update(msg2)
22 assert(err == OK)
23 var hmac = ctx.finish()
24 print(hmac.hex_encode())
25
26 [/gdscript]
27 [csharp]
28 using Godot;
29 using System.Diagnostics;
30
31 public partial class MyNode : Node
32 {
33 private HmacContext _ctx = new HmacContext();
34
35 public override void _Ready()
36 {
37 byte[] key = "supersecret".ToUtf8Buffer();
38 Error err = _ctx.Start(HashingContext.HashType.Sha256, key);
39 Debug.Assert(err == Error.Ok);
40 byte[] msg1 = "this is ".ToUtf8Buffer();
41 byte[] msg2 = "super duper secret".ToUtf8Buffer();
42 err = _ctx.Update(msg1);
43 Debug.Assert(err == Error.Ok);
44 err = _ctx.Update(msg2);
45 Debug.Assert(err == Error.Ok);
46 byte[] hmac = _ctx.Finish();
47 GD.Print(hmac.HexEncode());
48 }
49 }
50 [/csharp]
51 [/codeblocks]
52 </description>
53 <tutorials>
54 </tutorials>
55 <methods>
56 <method name="finish">
57 <return type="PackedByteArray" />
58 <description>
59 Returns the resulting HMAC. If the HMAC failed, an empty [PackedByteArray] is returned.
60 </description>
61 </method>
62 <method name="start">
63 <return type="int" enum="Error" />
64 <param index="0" name="hash_type" type="int" enum="HashingContext.HashType" />
65 <param index="1" name="key" type="PackedByteArray" />
66 <description>
67 Initializes the HMACContext. This method cannot be called again on the same HMACContext until [method finish] has been called.
68 </description>
69 </method>
70 <method name="update">
71 <return type="int" enum="Error" />
72 <param index="0" name="data" type="PackedByteArray" />
73 <description>
74 Updates the message to be HMACed. This can be called multiple times before [method finish] is called to append [param data] to the message, but cannot be called until [method start] has been called.
75 </description>
76 </method>
77 </methods>
78</class>