Nieuw met 2.84
svn path=/Slnkdwf/trunk/; revision=12540
This commit is contained in:
97
SlnkDWFCom/Crypto.cpp
Normal file
97
SlnkDWFCom/Crypto.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
// Crypto.cpp : Implementation of CCrypto
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "Crypto.h"
|
||||
|
||||
|
||||
// CCrypto
|
||||
|
||||
#include "hmac_sha1.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
// Base64_encode from http://www.adp-gmbh.ch/cpp/common/base64.html
|
||||
static const char *base64_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
static inline bool is_base64(unsigned char c) {
|
||||
return (isalnum(c) || (c == '+') || (c == '/'));
|
||||
}
|
||||
|
||||
CString base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len, BOOL padding) {
|
||||
CString ret;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
unsigned char char_array_3[3];
|
||||
unsigned char char_array_4[4];
|
||||
|
||||
while (in_len--) {
|
||||
char_array_3[i++] = *(bytes_to_encode++);
|
||||
if (i == 3) {
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for(i = 0; (i <4) ; i++)
|
||||
ret += base64_chars[char_array_4[i]];
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i)
|
||||
{
|
||||
for(j = i; j < 3; j++)
|
||||
char_array_3[j] = '\0';
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for (j = 0; (j < i + 1); j++)
|
||||
ret += base64_chars[char_array_4[j]];
|
||||
|
||||
while(padding && (i++ < 3))
|
||||
ret += '=';
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
STDMETHODIMP CCrypto::b64_hmac_sha1(BSTR key, BSTR data, BSTR* pVal)
|
||||
{
|
||||
BYTE digest[20];
|
||||
CString akey(key);
|
||||
CString adata(data);
|
||||
|
||||
CHMAC_SHA1 HMAC_SHA1 ;
|
||||
HMAC_SHA1.HMAC_SHA1((BYTE *)adata.GetBuffer(), adata.GetLength(), (BYTE *)akey.GetBuffer(), akey.GetLength(), digest) ;
|
||||
CString res = base64_encode(digest, 20, false); // no padding
|
||||
CComBSTR bstrString(res);
|
||||
return bstrString.CopyTo(pVal);
|
||||
}
|
||||
|
||||
const char *hex_tab = "0123456789abcdef";
|
||||
STDMETHODIMP CCrypto::hex_hmac_sha1(BSTR key, BSTR data, BSTR* pVal)
|
||||
{
|
||||
BYTE digest[20];
|
||||
CString akey(key);
|
||||
CString adata(data);
|
||||
|
||||
CHMAC_SHA1 HMAC_SHA1 ;
|
||||
HMAC_SHA1.HMAC_SHA1((BYTE *)adata.GetBuffer(), adata.GetLength(), (BYTE *)akey.GetBuffer(), akey.GetLength(), digest) ;
|
||||
CString res;
|
||||
|
||||
for(int i = 0; i < 20; i++)
|
||||
{
|
||||
res = res + hex_tab[digest[i] >> 4];
|
||||
res = res + hex_tab[digest[i] & 0xF];
|
||||
}
|
||||
|
||||
CComBSTR bstrString(res);
|
||||
return bstrString.CopyTo(pVal);
|
||||
}
|
||||
54
SlnkDWFCom/Crypto.h
Normal file
54
SlnkDWFCom/Crypto.h
Normal file
@@ -0,0 +1,54 @@
|
||||
// Crypto.h : Declaration of the CCrypto
|
||||
|
||||
#pragma once
|
||||
#include "resource.h" // main symbols
|
||||
|
||||
#include "SLNKDWF.h"
|
||||
|
||||
|
||||
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
|
||||
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// CCrypto
|
||||
|
||||
class ATL_NO_VTABLE CCrypto :
|
||||
public CComObjectRootEx<CComSingleThreadModel>,
|
||||
public CComCoClass<CCrypto, &CLSID_Crypto>,
|
||||
public IDispatchImpl<ICrypto, &IID_ICrypto, &LIBID_SLNKDWFLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
|
||||
{
|
||||
public:
|
||||
CCrypto()
|
||||
{
|
||||
}
|
||||
|
||||
DECLARE_REGISTRY_RESOURCEID(IDR_CRYPTO)
|
||||
|
||||
|
||||
BEGIN_COM_MAP(CCrypto)
|
||||
COM_INTERFACE_ENTRY(ICrypto)
|
||||
COM_INTERFACE_ENTRY(IDispatch)
|
||||
END_COM_MAP()
|
||||
|
||||
|
||||
|
||||
DECLARE_PROTECT_FINAL_CONSTRUCT()
|
||||
|
||||
HRESULT FinalConstruct()
|
||||
{
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
void FinalRelease()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
STDMETHOD(b64_hmac_sha1)(BSTR key, BSTR data, BSTR* pVal);
|
||||
STDMETHOD(hex_hmac_sha1)(BSTR key, BSTR data, BSTR* pVal);
|
||||
};
|
||||
|
||||
OBJECT_ENTRY_AUTO(__uuidof(Crypto), CCrypto)
|
||||
26
SlnkDWFCom/Crypto.rgs
Normal file
26
SlnkDWFCom/Crypto.rgs
Normal file
@@ -0,0 +1,26 @@
|
||||
HKCR
|
||||
{
|
||||
SLNKDWF.Crypto.1 = s 'Crypto Class'
|
||||
{
|
||||
CLSID = s '{0D6DF1E6-A665-4655-9674-062E9DF60F24}'
|
||||
}
|
||||
SLNKDWF.Crypto = s 'Crypto Class'
|
||||
{
|
||||
CLSID = s '{0D6DF1E6-A665-4655-9674-062E9DF60F24}'
|
||||
CurVer = s 'SLNKDWF.Crypto.1'
|
||||
}
|
||||
NoRemove CLSID
|
||||
{
|
||||
ForceRemove {0D6DF1E6-A665-4655-9674-062E9DF60F24} = s 'Crypto Class'
|
||||
{
|
||||
ProgID = s 'SLNKDWF.Crypto.1'
|
||||
VersionIndependentProgID = s 'SLNKDWF.Crypto'
|
||||
ForceRemove 'Programmable'
|
||||
InprocServer32 = s '%MODULE%'
|
||||
{
|
||||
val ThreadingModel = s 'Apartment'
|
||||
}
|
||||
'TypeLib' = s '{B6FCDE6E-141C-4601-B3AC-4DF4D5F25DF8}'
|
||||
}
|
||||
}
|
||||
}
|
||||
65
SlnkDWFCom/hmac_sha1.cpp
Normal file
65
SlnkDWFCom/hmac_sha1.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
//******************************************************************************
|
||||
//* HMAC_SHA1.cpp : Implementation of HMAC SHA1 algorithm
|
||||
//* Comfort to RFC 2104
|
||||
//*
|
||||
//******************************************************************************
|
||||
#include "stdafx.h"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "HMAC_SHA1.h"
|
||||
|
||||
|
||||
void CHMAC_SHA1::HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest)
|
||||
{
|
||||
memset(SHA1_Key, 0, SHA1_BLOCK_SIZE);
|
||||
|
||||
/* repeated 64 times for values in ipad and opad */
|
||||
memset(m_ipad, 0x36, sizeof(m_ipad));
|
||||
memset(m_opad, 0x5c, sizeof(m_opad));
|
||||
|
||||
/* STEP 1 */
|
||||
if (key_len > SHA1_BLOCK_SIZE)
|
||||
{
|
||||
CSHA1::Reset();
|
||||
CSHA1::Update((UINT_8 *)key, key_len);
|
||||
CSHA1::Final();
|
||||
|
||||
CSHA1::GetHash((UINT_8 *)SHA1_Key);
|
||||
}
|
||||
else
|
||||
memcpy(SHA1_Key, key, key_len);
|
||||
|
||||
/* STEP 2 */
|
||||
for (int i=0; i<sizeof(m_ipad); i++)
|
||||
{
|
||||
m_ipad[i] ^= SHA1_Key[i];
|
||||
}
|
||||
|
||||
/* STEP 3 */
|
||||
memcpy(AppendBuf1, m_ipad, sizeof(m_ipad));
|
||||
memcpy(AppendBuf1 + sizeof(m_ipad), text, text_len);
|
||||
|
||||
/* STEP 4 */
|
||||
CSHA1::Reset();
|
||||
CSHA1::Update((UINT_8 *)AppendBuf1, sizeof(m_ipad) + text_len);
|
||||
CSHA1::Final();
|
||||
|
||||
CSHA1::GetHash((UINT_8 *)szReport);
|
||||
|
||||
/* STEP 5 */
|
||||
for (int j=0; j<sizeof(m_opad); j++)
|
||||
{
|
||||
m_opad[j] ^= SHA1_Key[j];
|
||||
}
|
||||
|
||||
/* STEP 6 */
|
||||
memcpy(AppendBuf2, m_opad, sizeof(m_opad));
|
||||
memcpy(AppendBuf2 + sizeof(m_opad), szReport, SHA1_DIGEST_LENGTH);
|
||||
|
||||
/*STEP 7 */
|
||||
CSHA1::Reset();
|
||||
CSHA1::Update((UINT_8 *)AppendBuf2, sizeof(m_opad) + SHA1_DIGEST_LENGTH);
|
||||
CSHA1::Final();
|
||||
|
||||
CSHA1::GetHash((UINT_8 *)digest);
|
||||
}
|
||||
53
SlnkDWFCom/hmac_sha1.h
Normal file
53
SlnkDWFCom/hmac_sha1.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
100% free public domain implementation of the HMAC-SHA1 algorithm
|
||||
by Chien-Chung, Chung (Jim Chung) <jimchung1221@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __HMAC_SHA1_H__
|
||||
#define __HMAC_SHA1_H__
|
||||
|
||||
#include "SHA1.h"
|
||||
|
||||
typedef unsigned char BYTE ;
|
||||
|
||||
class CHMAC_SHA1 : public CSHA1
|
||||
{
|
||||
private:
|
||||
BYTE m_ipad[64];
|
||||
BYTE m_opad[64];
|
||||
|
||||
char * szReport ;
|
||||
char * SHA1_Key ;
|
||||
char * AppendBuf1 ;
|
||||
char * AppendBuf2 ;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
enum {
|
||||
SHA1_DIGEST_LENGTH = 20,
|
||||
SHA1_BLOCK_SIZE = 64,
|
||||
HMAC_BUF_LEN = 4096
|
||||
} ;
|
||||
|
||||
CHMAC_SHA1()
|
||||
:szReport(new char[HMAC_BUF_LEN]),
|
||||
AppendBuf1(new char[HMAC_BUF_LEN]),
|
||||
AppendBuf2(new char[HMAC_BUF_LEN]),
|
||||
SHA1_Key(new char[HMAC_BUF_LEN])
|
||||
{}
|
||||
|
||||
~CHMAC_SHA1()
|
||||
{
|
||||
delete[] szReport ;
|
||||
delete[] AppendBuf1 ;
|
||||
delete[] AppendBuf2 ;
|
||||
delete[] SHA1_Key ;
|
||||
}
|
||||
|
||||
void HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest);
|
||||
};
|
||||
|
||||
|
||||
#endif /* __HMAC_SHA1_H__ */
|
||||
Reference in New Issue
Block a user