hashing_context.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "hashing_context.h"
  31. #include "core/crypto/crypto_core.h"
  32. Error HashingContext::start(HashType p_type) {
  33. ERR_FAIL_COND_V(ctx != nullptr, ERR_ALREADY_IN_USE);
  34. _create_ctx(p_type);
  35. ERR_FAIL_NULL_V(ctx, ERR_UNAVAILABLE);
  36. switch (type) {
  37. case HASH_MD5:
  38. return ((CryptoCore::MD5Context *)ctx)->start();
  39. case HASH_SHA1:
  40. return ((CryptoCore::SHA1Context *)ctx)->start();
  41. case HASH_SHA256:
  42. return ((CryptoCore::SHA256Context *)ctx)->start();
  43. }
  44. return ERR_UNAVAILABLE;
  45. }
  46. Error HashingContext::update(const PackedByteArray &p_chunk) {
  47. ERR_FAIL_NULL_V(ctx, ERR_UNCONFIGURED);
  48. size_t len = p_chunk.size();
  49. ERR_FAIL_COND_V(len == 0, FAILED);
  50. const uint8_t *r = p_chunk.ptr();
  51. switch (type) {
  52. case HASH_MD5:
  53. return ((CryptoCore::MD5Context *)ctx)->update(&r[0], len);
  54. case HASH_SHA1:
  55. return ((CryptoCore::SHA1Context *)ctx)->update(&r[0], len);
  56. case HASH_SHA256:
  57. return ((CryptoCore::SHA256Context *)ctx)->update(&r[0], len);
  58. }
  59. return ERR_UNAVAILABLE;
  60. }
  61. PackedByteArray HashingContext::finish() {
  62. ERR_FAIL_NULL_V(ctx, PackedByteArray());
  63. PackedByteArray out;
  64. Error err = FAILED;
  65. switch (type) {
  66. case HASH_MD5:
  67. out.resize(16);
  68. err = ((CryptoCore::MD5Context *)ctx)->finish(out.ptrw());
  69. break;
  70. case HASH_SHA1:
  71. out.resize(20);
  72. err = ((CryptoCore::SHA1Context *)ctx)->finish(out.ptrw());
  73. break;
  74. case HASH_SHA256:
  75. out.resize(32);
  76. err = ((CryptoCore::SHA256Context *)ctx)->finish(out.ptrw());
  77. break;
  78. }
  79. _delete_ctx();
  80. ERR_FAIL_COND_V(err != OK, PackedByteArray());
  81. return out;
  82. }
  83. void HashingContext::_create_ctx(HashType p_type) {
  84. type = p_type;
  85. switch (type) {
  86. case HASH_MD5:
  87. ctx = memnew(CryptoCore::MD5Context);
  88. break;
  89. case HASH_SHA1:
  90. ctx = memnew(CryptoCore::SHA1Context);
  91. break;
  92. case HASH_SHA256:
  93. ctx = memnew(CryptoCore::SHA256Context);
  94. break;
  95. default:
  96. ctx = nullptr;
  97. }
  98. }
  99. void HashingContext::_delete_ctx() {
  100. switch (type) {
  101. case HASH_MD5:
  102. memdelete((CryptoCore::MD5Context *)ctx);
  103. break;
  104. case HASH_SHA1:
  105. memdelete((CryptoCore::SHA1Context *)ctx);
  106. break;
  107. case HASH_SHA256:
  108. memdelete((CryptoCore::SHA256Context *)ctx);
  109. break;
  110. }
  111. ctx = nullptr;
  112. }
  113. void HashingContext::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("start", "type"), &HashingContext::start);
  115. ClassDB::bind_method(D_METHOD("update", "chunk"), &HashingContext::update);
  116. ClassDB::bind_method(D_METHOD("finish"), &HashingContext::finish);
  117. BIND_ENUM_CONSTANT(HASH_MD5);
  118. BIND_ENUM_CONSTANT(HASH_SHA1);
  119. BIND_ENUM_CONSTANT(HASH_SHA256);
  120. }
  121. HashingContext::~HashingContext() {
  122. if (ctx != nullptr) {
  123. _delete_ctx();
  124. }
  125. }