2019-08-22 12:49:30 +00:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-03-01 00:44:37 +00:00
<class name= "HashingContext" inherits= "RefCounted" version= "4.1" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2019-08-22 12:49:30 +00:00
<brief_description >
2019-09-23 16:16:41 +00:00
Context to compute cryptographic hashes over multiple iterations.
2019-08-22 12:49:30 +00:00
</brief_description>
<description >
2019-09-23 16:16:41 +00:00
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.
2020-10-30 23:37:55 +00:00
[codeblocks]
[gdscript]
2023-01-31 17:21:09 +00:00
const CHUNK_SIZE = 1024
2019-09-23 16:16:41 +00:00
func hash_file(path):
# Check that file exists.
2023-01-30 14:21:36 +00:00
if not FileAccess.file_exists(path):
2019-09-23 16:16:41 +00:00
return
2023-01-30 14:21:36 +00:00
# Start a SHA-256 context.
var ctx = HashingContext.new()
ctx.start(HashingContext.HASH_SHA256)
2019-09-23 16:16:41 +00:00
# Open the file to hash.
2023-01-30 14:21:36 +00:00
var file = FileAccess.open(path, FileAccess.READ)
2019-09-23 16:16:41 +00:00
# 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))
2020-10-30 23:37:55 +00:00
[/gdscript]
[csharp]
public const int ChunkSize = 1024;
public void HashFile(string path)
{
// Check that file exists.
2023-01-31 17:21:09 +00:00
if (!FileAccess.FileExists(path))
2020-10-30 23:37:55 +00:00
{
return;
}
2023-01-31 17:21:09 +00:00
// Start a SHA-256 context.
var ctx = new HashingContext();
ctx.Start(HashingContext.HashType.Sha256);
2020-10-30 23:37:55 +00:00
// Open the file to hash.
2023-01-31 17:21:09 +00:00
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
2020-10-30 23:37:55 +00:00
// 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.
2023-01-31 17:21:09 +00:00
GD.PrintT(res.HexEncode(), (Variant)res);
2020-10-30 23:37:55 +00:00
}
[/csharp]
[/codeblocks]
2019-08-22 12:49:30 +00:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "finish" >
2021-07-30 13:28:05 +00:00
<return type= "PackedByteArray" />
2019-08-22 12:49:30 +00:00
<description >
2019-09-23 16:16:41 +00:00
Closes the current context, and return the computed hash.
2019-08-22 12:49:30 +00:00
</description>
</method>
<method name= "start" >
2021-07-30 13:28:05 +00:00
<return type= "int" enum= "Error" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "type" type= "int" enum= "HashingContext.HashType" />
2019-08-22 12:49:30 +00:00
<description >
2022-08-12 16:07:53 +00:00
Starts a new hash computation of the given [param type] (e.g. [constant HASH_SHA256] to start computation of a SHA-256).
2019-08-22 12:49:30 +00:00
</description>
</method>
<method name= "update" >
2021-07-30 13:28:05 +00:00
<return type= "int" enum= "Error" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "chunk" type= "PackedByteArray" />
2019-08-22 12:49:30 +00:00
<description >
2022-08-12 16:07:53 +00:00
Updates the computation with the given [param chunk] of data.
2019-08-22 12:49:30 +00:00
</description>
</method>
</methods>
<constants >
<constant name= "HASH_MD5" value= "0" enum= "HashType" >
2019-09-23 16:16:41 +00:00
Hashing algorithm: MD5.
2019-08-22 12:49:30 +00:00
</constant>
<constant name= "HASH_SHA1" value= "1" enum= "HashType" >
2019-09-23 16:16:41 +00:00
Hashing algorithm: SHA-1.
2019-08-22 12:49:30 +00:00
</constant>
<constant name= "HASH_SHA256" value= "2" enum= "HashType" >
2019-09-23 16:16:41 +00:00
Hashing algorithm: SHA-256.
2019-08-22 12:49:30 +00:00
</constant>
</constants>
</class>