Add SHA1 hash generator

This commit is contained in:
Don Ho 2023-06-13 19:35:06 +02:00
parent b0e849fe65
commit 3b823f358e
5 changed files with 121 additions and 5 deletions

View File

@ -18,7 +18,7 @@
#include "StaticDialog.h"
enum hashType {hash_md5, hash_sha256};
enum hashType {hash_md5, hash_sha256, hash_sha1};
LRESULT run_textEditProc(WNDPROC oldEditProc, HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

View File

@ -0,0 +1,104 @@
#include <cstdint>
#include <cstring>
#include <iostream>
uint32_t leftrotate(uint32_t x, uint32_t c) {
return (x << c) | (x >> (32-c));
}
uint32_t from_bytes(const unsigned char * p) {
return (((uint32_t)p[3]) << 24)
| (((uint32_t)p[2]) << 16)
| (((uint32_t)p[1]) << 8)
| ((uint32_t)p[0]);
}
void to_bytes(uint32_t val, unsigned char * p) {
p[3] = unsigned char(val >> 24);
p[2] = unsigned char(val >> 16);
p[1] = unsigned char(val >> 8);
p[0] = unsigned char(val >> 0);
}
void calc_sha1(uint8_t hash[20], const void *input, size_t len) {
uint32_t h0 = 0x67452301;
uint32_t h1 = 0xEFCDAB89;
uint32_t h2 = 0x98BADCFE;
uint32_t h3 = 0x10325476;
uint32_t h4 = 0xC3D2E1F0;
unsigned char *msg = NULL;
size_t new_len, offset;
uint32_t w[80];
uint32_t a, b, c, d, e, f, k, temp;
new_len = len + 1;
while (new_len % 64 != 56) {
new_len++;
}
msg = (unsigned char*)malloc(new_len + 8);
memcpy(msg, input, len);
msg[len] = 0x80;
for (offset = len + 1; offset < new_len; offset++) {
msg[offset] = 0;
}
to_bytes(uint32_t(len * 8), msg + new_len);
new_len += 8;
for(offset=0; offset<new_len; offset += (512/8)) {
for (unsigned int i = 0; i < 16; i++) {
w[i] = from_bytes(msg + offset + i*4);
}
for (unsigned int i = 16; i < 80; i++) {
w[i] = leftrotate(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1);
}
a = h0;
b = h1;
c = h2;
d = h3;
e = h4;
for (unsigned int i = 0; i < 80; i++) {
if (i < 20) {
f = (b & c) | ((~b) & d);
k = 0x5A827999;
} else if (i < 40) {
f = b ^ c ^ d;
k = 0x6ED9EBA1;
} else if (i < 60) {
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
} else {
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
temp = leftrotate(a,5) + f + e + k + w[i];
e = d;
d = c;
c = leftrotate(b,30);
b = a;
a = temp;
}
h0 += a;
h1 += b;
h2 += c;
h3 += d;
h4 += e;
}
free(msg);
to_bytes(h0, hash);
to_bytes(h1, hash + 4);
to_bytes(h2, hash + 8);
to_bytes(h3, hash +12);
to_bytes(h4, hash +16);
}

View File

@ -0,0 +1,3 @@
#pragma once
void calc_sha1(uint8_t hash[20], const void *input, size_t len);

View File

@ -33,6 +33,7 @@
#include "verifySignedfile.h"
#include "md5.h"
#include "sha-256.h"
#include "sha1.h"
using namespace std;
@ -3246,12 +3247,15 @@ void Notepad_plus::command(int id)
char *selectedStr = new char[strSize];
_pEditView->execute(SCI_GETSELTEXT, 0, reinterpret_cast<LPARAM>(selectedStr));
uint8_t sha2hash[32];
calc_sha_256(sha2hash, reinterpret_cast<const uint8_t*>(selectedStr), strlen(selectedStr));
//uint8_t sha2hash[32];
//calc_sha_256(sha2hash, reinterpret_cast<const uint8_t*>(selectedStr), strlen(selectedStr));
uint8_t sha1hash[20];
calc_sha1(sha1hash, reinterpret_cast<const uint8_t*>(selectedStr), strlen(selectedStr));
wchar_t sha2hashStr[65] = { '\0' };
for (size_t i = 0; i < 32; i++)
wsprintf(sha2hashStr + i * 2, TEXT("%02x"), sha2hash[i]);
//for (size_t i = 0; i < 32; i++)
for (size_t i = 0; i < 20; i++)
wsprintf(sha2hashStr + i * 2, TEXT("%02x"), sha1hash[i]);
str2Clipboard(sha2hashStr, _pPublicInterface->getHSelf());

View File

@ -102,6 +102,9 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>..\src;..\src\MISC;..\src\MISC\Common;..\src\MISC\Exception;..\src\MISC\md5;..\src\MISC\PluginsManager;..\src\MISC\Process;..\src\MISC\RegExt;..\src\MISC\sha1;..\src\MISC\sha2;..\src\MISC\SysMsg;..\src\ScintillaComponent;..\src\Win32Explr;..\src\WinControls;..\src\WinControls\AboutDlg;..\src\WinControls\AnsiCharPanel;..\src\WinControls\ClipboardHistory;..\src\WinControls\ColourPicker;..\src\WinControls\ContextMenu;..\src\WinControls\DockingWnd;..\src\WinControls\DocumentMap;..\src\WinControls\FileBrowser;..\src\WinControls\FindCharsInRange;..\src\WinControls\FunctionList;..\src\WinControls\Grid;..\src\WinControls\ImageListSet;..\src\WinControls\OpenSaveFileDialog;..\src\WinControls\PluginsAdmin;..\src\WinControls\Preference;..\src\WinControls\ProjectPanel;..\src\WinControls\ReadDirectoryChanges;..\src\WinControls\shortcut;..\src\WinControls\SplitterContainer;..\src\WinControls\StaticDialog;..\src\WinControls\StaticDialog\RunDlg;..\src\WinControls\StatusBar;..\src\WinControls\TabBar;..\src\WinControls\TaskList;..\src\WinControls\ToolBar;..\src\WinControls\ToolTip;..\src\WinControls\TrayIcon;..\src\WinControls\TreeView;..\src\WinControls\VerticalFileSwitcher;..\src\WinControls\WindowsDlg;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
</ItemDefinitionGroup>
@ -109,6 +112,7 @@
<ClCompile Include="..\src\DarkMode\DarkMode.cpp" />
<ClCompile Include="..\src\MISC\Common\verifySignedfile.cpp" />
<ClCompile Include="..\src\MISC\md5\md5Dlgs.cpp" />
<ClCompile Include="..\src\MISC\sha1\sha1.cpp" />
<ClCompile Include="..\src\MISC\sha2\sha-256.cpp" />
<ClCompile Include="..\src\NppDarkMode.cpp" />
<ClCompile Include="..\src\WinControls\AboutDlg\AboutDlg.cpp" />
@ -246,6 +250,7 @@
<ClInclude Include="..\src\MISC\md5\md5.h" />
<ClInclude Include="..\src\MISC\md5\md5Dlgs.h" />
<ClInclude Include="..\src\MISC\md5\md5Dlgs_rc.h" />
<ClInclude Include="..\src\MISC\sha1\sha1.h" />
<ClInclude Include="..\src\MISC\sha2\sha-256.h" />
<ClInclude Include="..\src\NppDarkMode.h" />
<ClInclude Include="..\src\ScintillaComponent\columnEditor_rc.h" />