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:
quietly-turning
2020-09-09 15:37:01 -04:00
parent fdef606b46
commit 72cd6edf5c
5 changed files with 71 additions and 6 deletions
+47
View File
@@ -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 );
}
};