1 /**
2 Context to compute cryptographic hashes over multiple iterations.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.hashingcontext;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.classdb;
24 import godot.reference;
25 /**
26 Context to compute cryptographic hashes over multiple iterations.
27 
28 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).
29 The $(D hashtype) enum shows the supported hashing algorithms.
30 
31 
32 const CHUNK_SIZE = 1024
33 
34 func hash_file(path):
35     var ctx = HashingContext.new()
36     var file = File.new()
37     # Start a SHA-256 context.
38     ctx.start(HashingContext.HASH_SHA256)
39     # Check that file exists.
40     if not file.file_exists(path):
41         return
42     # Open the file to hash.
43     file.open(path, File.READ)
44     # Update the context after reading each chunk.
45     while not file.eof_reached():
46         ctx.update(file.get_buffer(CHUNK_SIZE))
47     # Get the computed hash.
48     var res = ctx.finish()
49     # Print the result as hex string and array.
50     printt(res.hex_encode(), Array(res))
51 
52 
53 $(B Note:) Not available in HTML5 exports.
54 */
55 @GodotBaseClass struct HashingContext
56 {
57 	package(godot) enum string _GODOT_internal_name = "HashingContext";
58 public:
59 @nogc nothrow:
60 	union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; }
61 	alias _GODOT_base this;
62 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
63 	package(godot) __gshared bool _classBindingInitialized = false;
64 	package(godot) static struct GDNativeClassBinding
65 	{
66 		__gshared:
67 		@GodotName("finish") GodotMethod!(PoolByteArray) finish;
68 		@GodotName("start") GodotMethod!(GodotError, long) start;
69 		@GodotName("update") GodotMethod!(GodotError, PoolByteArray) update;
70 	}
71 	/// 
72 	pragma(inline, true) bool opEquals(in HashingContext other) const
73 	{ return _godot_object.ptr is other._godot_object.ptr; }
74 	/// 
75 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
76 	{ _godot_object.ptr = n; return null; }
77 	/// 
78 	pragma(inline, true) bool opEquals(typeof(null) n) const
79 	{ return _godot_object.ptr is n; }
80 	/// 
81 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
82 	mixin baseCasts;
83 	/// Construct a new instance of HashingContext.
84 	/// Note: use `memnew!HashingContext` instead.
85 	static HashingContext _new()
86 	{
87 		static godot_class_constructor constructor;
88 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("HashingContext");
89 		if(constructor is null) return typeof(this).init;
90 		return cast(HashingContext)(constructor());
91 	}
92 	@disable new(size_t s);
93 	/// 
94 	enum HashType : int
95 	{
96 		/**
97 		Hashing algorithm: MD5.
98 		*/
99 		hashMd5 = 0,
100 		/**
101 		Hashing algorithm: SHA-1.
102 		*/
103 		hashSha1 = 1,
104 		/**
105 		Hashing algorithm: SHA-256.
106 		*/
107 		hashSha256 = 2,
108 	}
109 	/// 
110 	enum Constants : int
111 	{
112 		hashMd5 = 0,
113 		hashSha1 = 1,
114 		hashSha256 = 2,
115 	}
116 	/**
117 	Closes the current context, and return the computed hash.
118 	*/
119 	PoolByteArray finish()
120 	{
121 		checkClassBinding!(typeof(this))();
122 		return ptrcall!(PoolByteArray)(GDNativeClassBinding.finish, _godot_object);
123 	}
124 	/**
125 	Starts a new hash computation of the given `type` (e.g. $(D constant HASH_SHA256) to start computation of a SHA-256).
126 	*/
127 	GodotError start(in long type)
128 	{
129 		checkClassBinding!(typeof(this))();
130 		return ptrcall!(GodotError)(GDNativeClassBinding.start, _godot_object, type);
131 	}
132 	/**
133 	Updates the computation with the given `chunk` of data.
134 	*/
135 	GodotError update(in PoolByteArray chunk)
136 	{
137 		checkClassBinding!(typeof(this))();
138 		return ptrcall!(GodotError)(GDNativeClassBinding.update, _godot_object, chunk);
139 	}
140 }