give Lua access to tomcrypt's SHA256
This adds Lua hooks to CryptManager for libtomcrypt's SHA256 hash function. It follows along with the C++ patterns Glenn wrote a decade and a half ago for CryptMananger's MD5 and SHA1 hooks. StepMania's CMakeProject-tomcrypt file has been updated to build with sha256 symbols. Invoking cmake to build with system tomcrypt works fine as-is. Note that these functions return binary formatted strings, but themers can convert to hexadecimal format using the global Lua function BinaryToHex().
This commit is contained in:
@@ -420,6 +420,37 @@ RString CryptManager::GetSHA1ForFile( RString fn )
|
||||
return RString( (const char *) digest, sizeof(digest) );
|
||||
}
|
||||
|
||||
RString CryptManager::GetSHA256ForString( RString sData )
|
||||
{
|
||||
unsigned char digest[32];
|
||||
|
||||
int iHash = register_hash( &sha256_desc );
|
||||
|
||||
hash_state hash;
|
||||
hash_descriptor[iHash].init( &hash );
|
||||
hash_descriptor[iHash].process( &hash, (const unsigned char *) sData.data(), sData.size() );
|
||||
hash_descriptor[iHash].done( &hash, digest );
|
||||
|
||||
return RString( (const char *) digest, sizeof(digest) );
|
||||
}
|
||||
|
||||
RString CryptManager::GetSHA256ForFile( RString fn )
|
||||
{
|
||||
RageFile file;
|
||||
if( !file.Open( fn, RageFile::READ ) )
|
||||
{
|
||||
LOG->Warn( "GetSHA256: Failed to open file '%s'", fn.c_str() );
|
||||
return RString();
|
||||
}
|
||||
int iHash = register_hash( &sha256_desc );
|
||||
ASSERT( iHash >= 0 );
|
||||
|
||||
unsigned char digest[32];
|
||||
HashFile( file, digest, iHash );
|
||||
|
||||
return RString( (const char *) digest, sizeof(digest) );
|
||||
}
|
||||
|
||||
RString CryptManager::GetPublicKeyFileName()
|
||||
{
|
||||
return PUBLIC_KEY_PATH;
|
||||
@@ -478,6 +509,20 @@ public:
|
||||
lua_pushlstring(L, sha1fout, sha1fout.size());
|
||||
return 1;
|
||||
}
|
||||
static int SHA256String( T* p, lua_State *L )
|
||||
{
|
||||
RString sha256out;
|
||||
sha256out = p->GetSHA256ForString(SArg(1));
|
||||
lua_pushlstring(L, sha256out, sha256out.size());
|
||||
return 1;
|
||||
}
|
||||
static int SHA256File( T* p, lua_State *L )
|
||||
{
|
||||
RString sha256fout;
|
||||
sha256fout = p->GetSHA256ForFile(SArg(1));
|
||||
lua_pushlstring(L, sha256fout, sha256fout.size());
|
||||
return 1;
|
||||
}
|
||||
static int GenerateRandomUUID( T* p, lua_State *L )
|
||||
{
|
||||
RString uuidOut;
|
||||
@@ -492,6 +537,8 @@ public:
|
||||
ADD_METHOD( MD5File );
|
||||
ADD_METHOD( SHA1String );
|
||||
ADD_METHOD( SHA1File );
|
||||
ADD_METHOD( SHA256String );
|
||||
ADD_METHOD( SHA256File );
|
||||
ADD_METHOD( GenerateRandomUUID );
|
||||
}
|
||||
};
|
||||
|
||||
+8
-6
@@ -24,10 +24,12 @@ public:
|
||||
static void GetRandomBytes( void *pData, int iBytes );
|
||||
static RString GenerateRandomUUID();
|
||||
|
||||
static RString GetMD5ForFile( RString fn ); // in binary
|
||||
static RString GetMD5ForString( RString sData ); // in binary
|
||||
static RString GetSHA1ForString( RString sData ); // in binary
|
||||
static RString GetSHA1ForFile( RString fn ); // in binary
|
||||
static RString GetMD5ForFile( RString fn ); // in binary
|
||||
static RString GetMD5ForString( RString sData ); // in binary
|
||||
static RString GetSHA1ForString( RString sData ); // in binary
|
||||
static RString GetSHA1ForFile( RString fn ); // in binary
|
||||
static RString GetSHA256ForString( RString sData ); // in binary
|
||||
static RString GetSHA256ForFile( RString fn ); // in binary
|
||||
|
||||
static RString GetPublicKeyFileName();
|
||||
|
||||
@@ -42,7 +44,7 @@ extern CryptManager* CRYPTMAN; // global and accessible from anywhere in our pro
|
||||
/*
|
||||
* (c) 2004 Chris Danford
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
@@ -52,7 +54,7 @@ extern CryptManager* CRYPTMAN; // global and accessible from anywhere in our pro
|
||||
* copyright notice(s) and this permission notice appear in all copies of
|
||||
* the Software and that both the above copyright notice(s) and this
|
||||
* permission notice appear in supporting documentation.
|
||||
*
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
||||
|
||||
Reference in New Issue
Block a user