1 | /**************************************************************************/ |
2 | /* hashing_context.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "hashing_context.h" |
32 | |
33 | #include "core/crypto/crypto_core.h" |
34 | |
35 | Error HashingContext::start(HashType p_type) { |
36 | ERR_FAIL_COND_V(ctx != nullptr, ERR_ALREADY_IN_USE); |
37 | _create_ctx(p_type); |
38 | ERR_FAIL_NULL_V(ctx, ERR_UNAVAILABLE); |
39 | switch (type) { |
40 | case HASH_MD5: |
41 | return ((CryptoCore::MD5Context *)ctx)->start(); |
42 | case HASH_SHA1: |
43 | return ((CryptoCore::SHA1Context *)ctx)->start(); |
44 | case HASH_SHA256: |
45 | return ((CryptoCore::SHA256Context *)ctx)->start(); |
46 | } |
47 | return ERR_UNAVAILABLE; |
48 | } |
49 | |
50 | Error HashingContext::update(PackedByteArray p_chunk) { |
51 | ERR_FAIL_NULL_V(ctx, ERR_UNCONFIGURED); |
52 | size_t len = p_chunk.size(); |
53 | ERR_FAIL_COND_V(len == 0, FAILED); |
54 | const uint8_t *r = p_chunk.ptr(); |
55 | switch (type) { |
56 | case HASH_MD5: |
57 | return ((CryptoCore::MD5Context *)ctx)->update(&r[0], len); |
58 | case HASH_SHA1: |
59 | return ((CryptoCore::SHA1Context *)ctx)->update(&r[0], len); |
60 | case HASH_SHA256: |
61 | return ((CryptoCore::SHA256Context *)ctx)->update(&r[0], len); |
62 | } |
63 | return ERR_UNAVAILABLE; |
64 | } |
65 | |
66 | PackedByteArray HashingContext::finish() { |
67 | ERR_FAIL_NULL_V(ctx, PackedByteArray()); |
68 | PackedByteArray out; |
69 | Error err = FAILED; |
70 | switch (type) { |
71 | case HASH_MD5: |
72 | out.resize(16); |
73 | err = ((CryptoCore::MD5Context *)ctx)->finish(out.ptrw()); |
74 | break; |
75 | case HASH_SHA1: |
76 | out.resize(20); |
77 | err = ((CryptoCore::SHA1Context *)ctx)->finish(out.ptrw()); |
78 | break; |
79 | case HASH_SHA256: |
80 | out.resize(32); |
81 | err = ((CryptoCore::SHA256Context *)ctx)->finish(out.ptrw()); |
82 | break; |
83 | } |
84 | _delete_ctx(); |
85 | ERR_FAIL_COND_V(err != OK, PackedByteArray()); |
86 | return out; |
87 | } |
88 | |
89 | void HashingContext::_create_ctx(HashType p_type) { |
90 | type = p_type; |
91 | switch (type) { |
92 | case HASH_MD5: |
93 | ctx = memnew(CryptoCore::MD5Context); |
94 | break; |
95 | case HASH_SHA1: |
96 | ctx = memnew(CryptoCore::SHA1Context); |
97 | break; |
98 | case HASH_SHA256: |
99 | ctx = memnew(CryptoCore::SHA256Context); |
100 | break; |
101 | default: |
102 | ctx = nullptr; |
103 | } |
104 | } |
105 | |
106 | void HashingContext::_delete_ctx() { |
107 | switch (type) { |
108 | case HASH_MD5: |
109 | memdelete((CryptoCore::MD5Context *)ctx); |
110 | break; |
111 | case HASH_SHA1: |
112 | memdelete((CryptoCore::SHA1Context *)ctx); |
113 | break; |
114 | case HASH_SHA256: |
115 | memdelete((CryptoCore::SHA256Context *)ctx); |
116 | break; |
117 | } |
118 | ctx = nullptr; |
119 | } |
120 | |
121 | void HashingContext::_bind_methods() { |
122 | ClassDB::bind_method(D_METHOD("start" , "type" ), &HashingContext::start); |
123 | ClassDB::bind_method(D_METHOD("update" , "chunk" ), &HashingContext::update); |
124 | ClassDB::bind_method(D_METHOD("finish" ), &HashingContext::finish); |
125 | BIND_ENUM_CONSTANT(HASH_MD5); |
126 | BIND_ENUM_CONSTANT(HASH_SHA1); |
127 | BIND_ENUM_CONSTANT(HASH_SHA256); |
128 | } |
129 | |
130 | HashingContext::~HashingContext() { |
131 | if (ctx != nullptr) { |
132 | _delete_ctx(); |
133 | } |
134 | } |
135 | |