Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMAC support #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Tests/Test_foxCryptoNG.prg
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,25 @@ Procedure Test_Hash_MD5
,m.lcHash ;
)

*========================================================================================
Procedure Test_HMAC_SHA256
Local loRef, lcHash
loRef = NewObject ("foxCryptoNG", "foxCryptoNG.prg")
lcHash = loRef.HashData ( "SHA256", "FoxPro rocks!", "Very secret key")
This.AssertEquals ( ;
"451DBF9AA457C2B3B51C0594588BE6FE09973C4A97B4E72395A6846219CD8873" ;
,m.lcHash ;
)


*========================================================================================
Procedure Test_HMAC_SHA512
Local loRef, lcHash
loRef = NewObject ("foxCryptoNG", "foxCryptoNG.prg")
lcHash = loRef.HashData ( "SHA512", "FoxPro rocks!", "Very secret key")
This.AssertEquals ( ;
"1691840B55E6A273996B5E1DA77AEA64C745AAEBE06AB68BE6EBF2A63FFC62D650150B3B2741DDC7B82643A675AAABD4A3FE72C49C6C93B4438FC9766A8C0D9C" ;
,m.lcHash ;
)

EndDefine
27 changes: 20 additions & 7 deletions foxCryptoNG.prg
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Define Class foxCryptoNG as Custom
#define BCRYPT_PAD_OAEP 0x00000004
#define BCRYPT_PAD_PSS 0x00000008
#define BCRYPT_PAD_PKCS1_OPTIONAL_HASH_OID 0x00000010
#define BCRYPT_ALG_HANDLE_HMAC_FLAG 0x00000008


*========================================================================================
Expand Down Expand Up @@ -87,7 +88,10 @@ Return m.lcHash
*========================================================================================
* Generic routine for hashing binary data.
*========================================================================================
Procedure HashData (tcAlgorithm, tcData)
Procedure HashData (tcAlgorithm, tcData, tcSecretKey)
* tcAlgorithm: MD5, SHA1, SHA256, SHA512
* tcData: arbitrary data
* tcSecretKey: if key is specified the routine performs HMAC algorithm with hash algorithm specified in tcAlgorithm parameter

*--------------------------------------------------------------------------------------
* Stop when we encounter a failure
Expand All @@ -102,7 +106,10 @@ Procedure HashData (tcAlgorithm, tcData)
lnAlg = 0
If m.llOK
llOK = BCryptOpenAlgorithmProvider( ;
@lnAlg, Strconv(m.tcAlgorithm+Chr(0),5), NULL, 0 ) == 0
@lnAlg, ;
Strconv(m.tcAlgorithm+Chr(0),5), ;
NULL, ;
Iif(Vartype(m.tcSecretKey)="C" AND Len(m.tcSecretKey) > 0, BCRYPT_ALG_HANDLE_HMAC_FLAG, 0) ) == 0
EndIf

*--------------------------------------------------------------------------------------
Expand Down Expand Up @@ -143,8 +150,14 @@ Procedure HashData (tcAlgorithm, tcData)
Local lnHash
lnHash = 0
If m.llOK
llOK = BCryptCreateHash( m.lnAlg, @lnHash, ;
lnHashObj, m.lnSizeObj, NULL, 0, 0 ) == 0
llOK = BCryptCreateHash( ;
m.lnAlg, ;
@lnHash, ;
lnHashObj, ;
m.lnSizeObj, ;
Iif(Vartype(m.tcSecretKey)="C" AND Len(m.tcSecretKey) > 0, m.tcSecretKey, NULL), ;
Iif(Vartype(m.tcSecretKey)="C" AND Len(m.tcSecretKey) > 0, Len(m.tcSecretKey), 0), ;
0 ) == 0
EndIf

*--------------------------------------------------------------------------------------
Expand Down Expand Up @@ -232,7 +245,7 @@ Procedure DeclareApiFunctions
Declare Long BCryptGetProperty in BCrypt.DLL ;
Long hObject, ;
String pszProperty, ;
Long @pbOutput, ;
Long @pbOutput, ;
Long cbOutput, ;
Long @pcbResult, ;
Long dwFlags
Expand Down Expand Up @@ -511,7 +524,7 @@ Procedure Encrypt_RSA (tcData, tcPublicKey)
EndIf

*--------------------------------------------------------------------------------------
* RSA is a fixed length algorithm. Plain text�and cipher text have fixed length. We
* RSA is a fixed length algorithm. Plain text�and cipher text have fixed length. We
* use PKCS1 padding to pad shorter blocks. Determine the size of ciphertext.
*--------------------------------------------------------------------------------------
Local lnSize
Expand Down Expand Up @@ -580,7 +593,7 @@ Procedure Decrypt_RSA (tcData, tcPrivateKey)
EndIf

*--------------------------------------------------------------------------------------
* RSA is a fixed length algorithm. Plain text�and cipher text have fixed length. We
* RSA is a fixed length algorithm. Plain text�and cipher text have fixed length. We
* use PKCS1 padding to pad shorter blocks. Determine the size of ciphertext.
*--------------------------------------------------------------------------------------
Local lnSize
Expand Down