summaryrefslogtreecommitdiff
path: root/doc/classes/HashingContext.xml
blob: f470f1611d675f1035dad68126fbc7dc960c11ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0" encoding="UTF-8" ?>
<class name="HashingContext" inherits="RefCounted" version="4.0">
	<brief_description>
		Context to compute cryptographic hashes over multiple iterations.
	</brief_description>
	<description>
		The HashingContext class provides an interface for computing cryptographic hashes over multiple iterations. This is useful for example when computing hashes of big files (so you don't have to load them all in memory), network streams, and data streams in general (so you don't have to hold buffers).
		The [enum HashType] enum shows the supported hashing algorithms.
		[codeblocks]
		[gdscript]
		const CHUNK_SIZE = 102

		func hash_file(path):
		    var ctx = HashingContext.new()
		    var file = File.new()
		    # Start a SHA-256 context.
		    ctx.start(HashingContext.HASH_SHA256)
		    # Check that file exists.
		    if not file.file_exists(path):
		        return
		    # Open the file to hash.
		    file.open(path, File.READ)
		    # Update the context after reading each chunk.
		    while not file.eof_reached():
		        ctx.update(file.get_buffer(CHUNK_SIZE))
		    # Get the computed hash.
		    var res = ctx.finish()
		    # Print the result as hex string and array.
		    printt(res.hex_encode(), Array(res))
		[/gdscript]
		[csharp]
		public const int ChunkSize = 1024;

		public void HashFile(string path)
		{
		    var ctx = new HashingContext();
		    var file = new File();
		    // Start a SHA-256 context.
		    ctx.Start(HashingContext.HashType.Sha256);
		    // Check that file exists.
		    if (!file.FileExists(path))
		    {
		        return;
		    }
		    // Open the file to hash.
		    file.Open(path, File.ModeFlags.Read);
		    // Update the context after reading each chunk.
		    while (!file.EofReached())
		    {
		        ctx.Update(file.GetBuffer(ChunkSize));
		    }
		    // Get the computed hash.
		    byte[] res = ctx.Finish();
		    // Print the result as hex string and array.

		    GD.PrintT(res.HexEncode(), res);
		}
		[/csharp]
		[/codeblocks]
		[b]Note:[/b] Not available in HTML5 exports.
	</description>
	<tutorials>
	</tutorials>
	<methods>
		<method name="finish">
			<return type="PackedByteArray" />
			<description>
				Closes the current context, and return the computed hash.
			</description>
		</method>
		<method name="start">
			<return type="int" enum="Error" />
			<argument index="0" name="type" type="int" enum="HashingContext.HashType" />
			<description>
				Starts a new hash computation of the given [code]type[/code] (e.g. [constant HASH_SHA256] to start computation of a SHA-256).
			</description>
		</method>
		<method name="update">
			<return type="int" enum="Error" />
			<argument index="0" name="chunk" type="PackedByteArray" />
			<description>
				Updates the computation with the given [code]chunk[/code] of data.
			</description>
		</method>
	</methods>
	<constants>
		<constant name="HASH_MD5" value="0" enum="HashType">
			Hashing algorithm: MD5.
		</constant>
		<constant name="HASH_SHA1" value="1" enum="HashType">
			Hashing algorithm: SHA-1.
		</constant>
		<constant name="HASH_SHA256" value="2" enum="HashType">
			Hashing algorithm: SHA-256.
		</constant>
	</constants>
</class>