core_compute_hash.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - compute hash
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define RAYGUI_IMPLEMENTATION
  17. #include "raygui.h"
  18. //----------------------------------------------------------------------------------
  19. // Module Functions Declaration
  20. //----------------------------------------------------------------------------------
  21. static char *GetDataAsHexText(const unsigned int *data, int dataSize);
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. InitWindow(screenWidth, screenHeight, "raylib [core] example - compute hash");
  32. // UI controls variables
  33. char textInput[96] = "The quick brown fox jumps over the lazy dog.";
  34. bool textBoxEditMode = false;
  35. bool btnComputeHashes = false;
  36. // Data hash values
  37. unsigned int hashCRC32 = 0;
  38. unsigned int *hashMD5 = NULL;
  39. unsigned int *hashSHA1 = NULL;
  40. unsigned int *hashSHA256 = NULL;
  41. // Base64 encoded data
  42. char *base64Text = NULL;
  43. int base64TextSize = 0;
  44. SetTargetFPS(60);
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. if (btnComputeHashes)
  52. {
  53. int textInputLen = strlen(textInput);
  54. // Encode data to Base64 string (includes NULL terminator), memory must be MemFree()
  55. base64Text = EncodeDataBase64((unsigned char *)textInput, textInputLen, &base64TextSize);
  56. hashCRC32 = ComputeCRC32((unsigned char *)textInput, textInputLen); // Compute CRC32 hash code (4 bytes)
  57. hashMD5 = ComputeMD5((unsigned char *)textInput, textInputLen); // Compute MD5 hash code, returns static int[4] (16 bytes)
  58. hashSHA1 = ComputeSHA1((unsigned char *)textInput, textInputLen); // Compute SHA1 hash code, returns static int[5] (20 bytes)
  59. hashSHA256 = ComputeSHA256((unsigned char *)textInput, textInputLen); // Compute SHA256 hash code, returns static int[8] (32 bytes)
  60. }
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. GuiSetStyle(DEFAULT, TEXT_SIZE, 20);
  67. GuiSetStyle(DEFAULT, TEXT_SPACING, 2);
  68. GuiLabel((Rectangle){ 40, 26, 720, 32 }, "INPUT DATA (TEXT):");
  69. GuiSetStyle(DEFAULT, TEXT_SPACING, 1);
  70. GuiSetStyle(DEFAULT, TEXT_SIZE, 10);
  71. if (GuiTextBox((Rectangle){ 40, 64, 720, 32 }, textInput, 95, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  72. btnComputeHashes = GuiButton((Rectangle){ 40, 64 + 40, 720, 32 }, "COMPUTE INPUT DATA HASHES");
  73. GuiSetStyle(DEFAULT, TEXT_SIZE, 20);
  74. GuiSetStyle(DEFAULT, TEXT_SPACING, 2);
  75. GuiLabel((Rectangle){ 40, 160, 720, 32 }, "INPUT DATA HASH VALUES:");
  76. GuiSetStyle(DEFAULT, TEXT_SPACING, 1);
  77. GuiSetStyle(DEFAULT, TEXT_SIZE, 10);
  78. GuiSetStyle(TEXTBOX, TEXT_READONLY, 1);
  79. GuiLabel((Rectangle){ 40, 200, 120, 32 }, "CRC32 [32 bit]:");
  80. GuiTextBox((Rectangle){ 40 + 120, 200, 720 - 120, 32 }, GetDataAsHexText(&hashCRC32, 1), 120, false);
  81. GuiLabel((Rectangle){ 40, 200 + 36, 120, 32 }, "MD5 [128 bit]:");
  82. GuiTextBox((Rectangle){ 40 + 120, 200 + 36, 720 - 120, 32 }, GetDataAsHexText(hashMD5, 4), 120, false);
  83. GuiLabel((Rectangle){ 40, 200 + 36*2, 120, 32 }, "SHA1 [160 bit]:");
  84. GuiTextBox((Rectangle){ 40 + 120, 200 + 36*2, 720 - 120, 32 }, GetDataAsHexText(hashSHA1, 5), 120, false);
  85. GuiLabel((Rectangle){ 40, 200 + 36*3, 120, 32 }, "SHA256 [256 bit]:");
  86. GuiTextBox((Rectangle){ 40 + 120, 200 + 36*3, 720 - 120, 32 }, GetDataAsHexText(hashSHA256, 8), 120, false);
  87. GuiSetState(STATE_FOCUSED);
  88. GuiLabel((Rectangle){ 40, 200 + 36*5 - 30, 320, 32 }, "BONUS - BAS64 ENCODED STRING:");
  89. GuiSetState(STATE_NORMAL);
  90. GuiLabel((Rectangle){ 40, 200 + 36*5, 120, 32 }, "BASE64 ENCODING:");
  91. GuiTextBox((Rectangle){ 40 + 120, 200 + 36*5, 720 - 120, 32 }, base64Text, 120, false);
  92. GuiSetStyle(TEXTBOX, TEXT_READONLY, 0);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }
  96. // De-Initialization
  97. //--------------------------------------------------------------------------------------
  98. MemFree(base64Text); // Free Base64 text data
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }
  103. //----------------------------------------------------------------------------------
  104. // Module Functions Definition
  105. //----------------------------------------------------------------------------------
  106. static char *GetDataAsHexText(const unsigned int *data, int dataSize)
  107. {
  108. static char text[128] = { 0 };
  109. memset(text, 0, 128);
  110. if ((data != NULL) && (dataSize > 0) && (dataSize < ((128/8) - 1)))
  111. {
  112. for (int i = 0; i < dataSize; i++) TextCopy(text + i*8, TextFormat("%08X", data[i]));
  113. }
  114. else TextCopy(text, "00000000");
  115. return text;
  116. }