2019-08-22 14:49:30 +02:00
<?xml version="1.0" encoding="UTF-8" ?>
2020-02-01 02:03:48 +01:00
<class name= "HashingContext" inherits= "Reference" version= "4.0" >
2019-08-22 14:49:30 +02:00
<brief_description >
2019-09-23 18:16:41 +02:00
Context to compute cryptographic hashes over multiple iterations.
2019-08-22 14:49:30 +02:00
</brief_description>
<description >
2019-09-23 18:16:41 +02: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-31 00:37:55 +01:00
[codeblocks]
[gdscript]
const CHUNK_SIZE = 102
2019-09-23 18:16:41 +02:00
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))
2020-10-31 00:37:55 +01:00
[/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]
2020-01-14 13:54:31 +01:00
[b]Note:[/b] Not available in HTML5 exports.
2019-08-22 14:49:30 +02:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "finish" >
2020-02-18 13:59:24 +01:00
<return type= "PackedByteArray" >
2019-08-22 14:49:30 +02:00
</return>
<description >
2019-09-23 18:16:41 +02:00
Closes the current context, and return the computed hash.
2019-08-22 14:49:30 +02:00
</description>
</method>
<method name= "start" >
<return type= "int" enum= "Error" >
</return>
<argument index= "0" name= "type" type= "int" enum= "HashingContext.HashType" >
</argument>
<description >
2019-09-23 18:16:41 +02:00
Starts a new hash computation of the given [code]type[/code] (e.g. [constant HASH_SHA256] to start computation of a SHA-256).
2019-08-22 14:49:30 +02:00
</description>
</method>
<method name= "update" >
<return type= "int" enum= "Error" >
</return>
2020-02-18 13:59:24 +01:00
<argument index= "0" name= "chunk" type= "PackedByteArray" >
2019-08-22 14:49:30 +02:00
</argument>
<description >
2019-09-23 18:16:41 +02:00
Updates the computation with the given [code]chunk[/code] of data.
2019-08-22 14:49:30 +02:00
</description>
</method>
</methods>
<constants >
<constant name= "HASH_MD5" value= "0" enum= "HashType" >
2019-09-23 18:16:41 +02:00
Hashing algorithm: MD5.
2019-08-22 14:49:30 +02:00
</constant>
<constant name= "HASH_SHA1" value= "1" enum= "HashType" >
2019-09-23 18:16:41 +02:00
Hashing algorithm: SHA-1.
2019-08-22 14:49:30 +02:00
</constant>
<constant name= "HASH_SHA256" value= "2" enum= "HashType" >
2019-09-23 18:16:41 +02:00
Hashing algorithm: SHA-256.
2019-08-22 14:49:30 +02:00
</constant>
</constants>
</class>