Merge pull request #2053 from quietly-turning/sha256

give CryptManager and Lua access to tomcrypt's SHA256
This commit is contained in:
quietly-turning
2020-09-09 16:46:35 -04:00
committed by GitHub
6 changed files with 84 additions and 18 deletions
+3
View File
@@ -12,6 +12,7 @@
<Function name='BGFitChoiceExample'/> <Function name='BGFitChoiceExample'/>
<Function name='BGFitInputActor'/> <Function name='BGFitInputActor'/>
<Function name='Basename'/> <Function name='Basename'/>
<Function name='BinaryToHex'/>
<Function name='BoostColor'/> <Function name='BoostColor'/>
<Function name='Brightness'/> <Function name='Brightness'/>
<Function name='Center1Player'/> <Function name='Center1Player'/>
@@ -757,6 +758,8 @@
<Function name='MD5String'/> <Function name='MD5String'/>
<Function name='SHA1File'/> <Function name='SHA1File'/>
<Function name='SHA1String'/> <Function name='SHA1String'/>
<Function name='SHA256File'/>
<Function name='SHA256String'/>
</Class> </Class>
<Class name='CubicSplineN'> <Class name='CubicSplineN'>
<Function name='solve'/> <Function name='solve'/>
+12
View File
@@ -74,6 +74,10 @@ save yourself some time, copy this for undocumented things:
<Function name='Basename' return='string' arguments='string path'> <Function name='Basename' return='string' arguments='string path'>
Returns the last named component of <code>path</code>. For example, passing in <code>"/path/to/file.txt"</code> will return <code>"file.txt"</code>, while <code>"/path/to/some/directory/"</code> will return <code>"directory"</code>. Returns the last named component of <code>path</code>. For example, passing in <code>"/path/to/file.txt"</code> will return <code>"file.txt"</code>, while <code>"/path/to/some/directory/"</code> will return <code>"directory"</code>.
</Function> </Function>
<Function name='BinaryToHex' return='string' arguments='string s'>
Converts a binary formatted string to hexadecimal format.
This can be useful in conjunction with <Link class="CryptManager" />'s MD5 and SHA functions.
</Function>
<Function name='BoostColor' theme='_fallback' return='color' arguments='color c, float fBoost'> <Function name='BoostColor' theme='_fallback' return='color' arguments='color c, float fBoost'>
[02 Colors.lua] Returns the color that results from multiplying <code>c</code>'s R, G, and B values by <code>fBoost</code>. [02 Colors.lua] Returns the color that results from multiplying <code>c</code>'s R, G, and B values by <code>fBoost</code>.
</Function> </Function>
@@ -2510,6 +2514,14 @@ Def.BitmapText{
<Function name='SHA1String' return='string' arguments='string s'> <Function name='SHA1String' return='string' arguments='string s'>
Returns the SHA-1 hash for <code>s</code>. Returns the SHA-1 hash for <code>s</code>.
</Function> </Function>
<Function name='SHA256File' return='string' arguments='string sPath'>
Returns the SHA-256 hash for the file at <code>sPath</code> as a binary formatted string.<br />
You can use <Link class='GLOBAL' function='BinaryToHex' /> to convert to hexadecimal format.
</Function>
<Function name='SHA256String' return='string' arguments='string s'>
Returns the SHA-256 hash for <code>s</code> as a binary formatted string.<br />
You can use <Link class='GLOBAL' function='BinaryToHex' /> to convert to hexadecimal format.
</Function>
</Class> </Class>
<Class name='CubicSplineN'> <Class name='CubicSplineN'>
<Description> <Description>
+1
View File
@@ -823,6 +823,7 @@ else()
sm_add_compile_definition("tomcrypt" LTC_DEVRANDOM) sm_add_compile_definition("tomcrypt" LTC_DEVRANDOM)
# Common formulas used by our app. # Common formulas used by our app.
sm_add_compile_definition("tomcrypt" LTC_SHA256)
sm_add_compile_definition("tomcrypt" LTC_SHA1) sm_add_compile_definition("tomcrypt" LTC_SHA1)
sm_add_compile_definition("tomcrypt" LTC_MD5) sm_add_compile_definition("tomcrypt" LTC_MD5)
+47
View File
@@ -420,6 +420,37 @@ RString CryptManager::GetSHA1ForFile( RString fn )
return RString( (const char *) digest, sizeof(digest) ); 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() RString CryptManager::GetPublicKeyFileName()
{ {
return PUBLIC_KEY_PATH; return PUBLIC_KEY_PATH;
@@ -478,6 +509,20 @@ public:
lua_pushlstring(L, sha1fout, sha1fout.size()); lua_pushlstring(L, sha1fout, sha1fout.size());
return 1; 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 ) static int GenerateRandomUUID( T* p, lua_State *L )
{ {
RString uuidOut; RString uuidOut;
@@ -492,6 +537,8 @@ public:
ADD_METHOD( MD5File ); ADD_METHOD( MD5File );
ADD_METHOD( SHA1String ); ADD_METHOD( SHA1String );
ADD_METHOD( SHA1File ); ADD_METHOD( SHA1File );
ADD_METHOD( SHA256String );
ADD_METHOD( SHA256File );
ADD_METHOD( GenerateRandomUUID ); ADD_METHOD( GenerateRandomUUID );
} }
}; };
+6 -4
View File
@@ -24,10 +24,12 @@ public:
static void GetRandomBytes( void *pData, int iBytes ); static void GetRandomBytes( void *pData, int iBytes );
static RString GenerateRandomUUID(); static RString GenerateRandomUUID();
static RString GetMD5ForFile( RString fn ); // in binary static RString GetMD5ForFile( RString fn ); // in binary
static RString GetMD5ForString( RString sData ); // in binary static RString GetMD5ForString( RString sData ); // in binary
static RString GetSHA1ForString( RString sData ); // in binary static RString GetSHA1ForString( RString sData ); // in binary
static RString GetSHA1ForFile( RString fn ); // 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(); static RString GetPublicKeyFileName();
+1
View File
@@ -2436,6 +2436,7 @@ LuaFunction( URLEncode, URLEncode( SArg(1) ) );
LuaFunction( PrettyPercent, PrettyPercent( FArg(1), FArg(2) ) ); LuaFunction( PrettyPercent, PrettyPercent( FArg(1), FArg(2) ) );
//LuaFunction( IsHexVal, IsHexVal( SArg(1) ) ); //LuaFunction( IsHexVal, IsHexVal( SArg(1) ) );
LuaFunction( lerp, lerp(FArg(1), FArg(2), FArg(3)) ); LuaFunction( lerp, lerp(FArg(1), FArg(2), FArg(3)) );
LuaFunction( BinaryToHex, BinaryToHex( SArg(1) ) );
int LuaFunc_commify(lua_State* L); int LuaFunc_commify(lua_State* L);
int LuaFunc_commify(lua_State* L) int LuaFunc_commify(lua_State* L)