SHA-1


SHA-1

The Secure Hash Algorithm 1 is a function designed to produce an output string (called hash) from an input message. That is, given an input message, SHA-1 produces an output Hash. The main feature of hash algorithms is that if the input message is altered, then the Hash must be different.
El Algoritmo de Hash Seguro es una función diseñada para producir una cadena de texto (llamada hash) desde un mensaje de entrada. Esto es, dado un mensaje de entrada, SHA-1 produce un hash de salida. La característica principal de los algoritmos hash es que si el mensaje de entrada se altera, entonces el Hash debe ser diferente.

Tip

Cuando se descarga un archivo de la Internet, el sitio puede ofrecer un Hash asociado con el archivo. En Microsoft Windows, usted puede usar el menú de contexto y el Hash para verificar que el archivo no ha sido alterado.

Problem 1
Create a Wintempla Dialog application called Agency to generate a SHA-1 hash from a given message using Windows Cryptography WITHOUT Wintempla classes. You need to edit the stdafx.h file to include Wincrypt.h.
Cree una aplicación de diálogo de Wintempla llamada Agency para generar un hash SHA-1 desde un mensaje dado usando Windows Cryptography sin usar las clases de Wintempla. Usted necesita editar el archivo stdafx.h para incluir Wincrypt.h.

AgencyRun

stdafx.h
...
//
#include "Wintempla.h"
#include "WintemplaWin.h"
using namespace std;
#include <Wincrypt.h>
...


Agency.cpp
...
void Agency::Window_Open(Win::Event& e)
{
}

void Agency::btGenerateHash_Click(Win::Event& e)
{
     //_______________________________________________________________________ 1. Cryptographic Provider
     HCRYPTPROV cryptProvider = NULL;
     if (::CryptAcquireContext(&cryptProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == 0)
     {
          this->MessageBox(L"Agency::CryptAcquireContext", L"Error", MB_OK | MB_ICONERROR);
          return;
     }
     //________________________________________________________________________ 2. Hash
     HCRYPTHASH cryptHash = NULL;
     ALG_ID hashAlgorithm = CALG_SHA1; //CALG_MD5, CALG_SHA_256, ...
     if (::CryptCreateHash(cryptProvider, hashAlgorithm, 0, 0, &cryptHash) == FALSE)
     {
          Sys::DisplayLastError(hWnd, L"CryptCreateHash");
          ::CryptReleaseContext(cryptProvider, 0);
          return;
     }
     //_______________________________________________________________________ 3. Process Input Message
     string message;
     Sys::Convert::WstringToString(tbxInput.Text, message);
     if (::CryptHashData(cryptHash, (const BYTE*)message.c_str(), (DWORD)message.size(), 0) == FALSE)
     {
          Sys::DisplayLastError(hWnd, L"CryptHashData");
          ::CryptDestroyHash(cryptHash);
          ::CryptReleaseContext(cryptProvider, 0);
          return ;
     }
     //______________________________________________________________________ 4. Get Output Length
     DWORD output_len = 0;
     if (::CryptGetHashParam(cryptHash, HP_HASHVAL, NULL, &output_len, 0) == FALSE)
     {
          Sys::DisplayLastError(hWnd, L"CryptHashData");
          ::CryptDestroyHash(cryptHash);
          ::CryptReleaseContext(cryptProvider, 0);
          return ;
     }
     //______________________________________________________________________ 5. Get Output
     BYTE * output = new BYTE[output_len];
     if (::CryptGetHashParam(cryptHash, HP_HASHVAL, output, &output_len, 0) == FALSE)
     {
          Sys::DisplayLastError(hWnd, L"CryptHashData");
          ::CryptDestroyHash(cryptHash);
          ::CryptReleaseContext(cryptProvider, 0);
          return ;
     }
     else
     {
          wchar_t text[64];
          for (DWORD i = 0; i < output_len; i++)
          {
               _snwprintf_s(text, 64, _TRUNCATE, L"%2.2x", output[i]);
               tbxOutput.Text += text;
          }
     }
     //________________________________________________________________________ 6. Clean up
     ::CryptDestroyHash(cryptHash);
     ::CryptReleaseContext(cryptProvider, 0);
}


Problem 2
Create a Wintempla Dialog application called AgencyX to generate a SHA-1 hash from a given message using Windows Cryptography and Wintempla classes.
Cree una aplicación de diálogo de Wintempla llamada AgencyX para generar un hash SHA-1 desde un mensaje dado usando Windows Cryptography y las clases de Wintempla.

Step A
Edit the stdafx.h file to activate Windows Cryptography using Wintempla classes (this step may not be necessary).
Edite el archivo stdafx.h para activar Windows Cryptography usando las clases de Wintempla (este paso podría no ser necesario.)

stdafx.h
...
//_________________________________________ Cryptography
#define WIN_CRYPTOGRAPHY
...


Step B
Edit the AgencyX.cpp file as shown.
Edite el archivo AgencyX.cpp como se muestra.

AgencyX.cpp
...
void AgencyX::Window_Open(Win::Event& e)
{
}

void AgencyX::btGenerateHash_Click(Win::Event& e)
{
     const unsigned int hashLen = 64;
     unsigned char hash[hashLen];
     string message;
     Sys::Convert::WstringToString(tbxMessage.Text, message);
     //
     Crypt::Assistant assistant;
     const int outputLen = assistant.CreateHash(CALG_SHA1, (const unsigned char*)message.c_str(), (unsigned int)message.size(), hash, hashLen);
     if (outputLen <= 0)
     {
          this->MessageBox(L"Unable to create hash", L"AgencyX", MB_OK | MB_ICONERROR);
          return;
     }
     wchar_t text[64];
     for (int i = 0; i < outputLen; i++)
     {
          _snwprintf_s(text, 64, _TRUNCATE, L"%2.2x", hash[i]);
          tbxOutput.Text += text;
     }
}


AgencyXRun

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home