From fdef606b4607dcc6c589227fd0fc5ab0fd4eebec Mon Sep 17 00:00:00 2001 From: quietly turning Date: Fri, 4 Sep 2020 15:41:33 -0400 Subject: [PATCH 1/2] expose RageUtil's BinaryToHex to Lua The hashing functions that CryptManger currently exposes to Lua return hash strings formatted in binary. Themes using those hash functions may want to compare hex strings. RageUtil already had a BinaryToHex utility; this commit exposes it as a global function, following along with other utility functions in RageUtil. --- src/RageUtil.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/RageUtil.cpp b/src/RageUtil.cpp index 445f4a776d..71cdef73a9 100644 --- a/src/RageUtil.cpp +++ b/src/RageUtil.cpp @@ -218,7 +218,7 @@ bool IsHexVal( const RString &s ) return false; for( size_t i=0; i < s.size(); ++i ) - if( !(s[i] >= '0' && s[i] <= '9') && + if( !(s[i] >= '0' && s[i] <= '9') && !(toupper(s[i]) >= 'A' && toupper(s[i]) <= 'F')) return false; @@ -339,7 +339,7 @@ RString PrettyPercent( float fNumerator, float fDenominator) return ssprintf("%0.2f%%",fNumerator/fDenominator*100); } -RString Commify( int iNum ) +RString Commify( int iNum ) { RString sNum = ssprintf("%d",iNum); return Commify( sNum ); @@ -494,7 +494,7 @@ RString vssprintf( const char *szFormat, va_list argList ) /* OK */ sStr.assign(buf, used); } - + delete [] buf; if (used != -1) { @@ -956,9 +956,9 @@ void splitpath( const RString &sPath, RString &sDir, RString &sFilename, RString vector asMatches; /* - * One level of escapes for the regex, one for C. Ew. + * One level of escapes for the regex, one for C. Ew. * This is really: - * ^(.*[\\/])?(.*)$ + * ^(.*[\\/])?(.*)$ */ static Regex sep("^(.*[\\\\/])?(.*)$"); bool bCheck = sep.Compare( sPath, asMatches ); @@ -1248,10 +1248,10 @@ float calc_stddev( const float *pStart, const float *pEnd, bool bSample ) bool CalcLeastSquares( const vector< pair > &vCoordinates, float &fSlope, float &fIntercept, float &fError ) { - if( vCoordinates.empty() ) + if( vCoordinates.empty() ) return false; float fSumXX = 0.0f, fSumXY = 0.0f, fSumX = 0.0f, fSumY = 0.0f; - for( unsigned i = 0; i < vCoordinates.size(); ++i ) + for( unsigned i = 0; i < vCoordinates.size(); ++i ) { fSumXX += vCoordinates[i].first * vCoordinates[i].first; fSumXY += vCoordinates[i].first * vCoordinates[i].second; @@ -1263,7 +1263,7 @@ bool CalcLeastSquares( const vector< pair > &vCoordinates, fIntercept = (fSumXX * fSumY - fSumX * fSumXY) / fDenominator; fError = 0.0f; - for( unsigned i = 0; i < vCoordinates.size(); ++i ) + for( unsigned i = 0; i < vCoordinates.size(); ++i ) { const float fOneError = fIntercept + fSlope * vCoordinates[i].first - vCoordinates[i].second; fError += fOneError * fOneError; @@ -1916,7 +1916,7 @@ wstring RStringToWstring( const RString &s ) ++start; continue; } - + wchar_t ch = L'\0'; if( !utf8_to_wchar( s.data(), s.size(), start, ch ) ) ch = INVALID_CHAR; @@ -2149,13 +2149,13 @@ RString Dirname( const RString &dir ) return dir.substr(0, pos+1); } -RString Capitalize( const RString &s ) +RString Capitalize( const RString &s ) { if( s.empty() ) return RString(); char *buf = const_cast(s.c_str()); - + UnicodeDoUpper( buf, s.size(), g_UpperCase ); return buf; @@ -2285,7 +2285,7 @@ void CollapsePath( RString &sPath, bool bRemoveLeadingDot ) sOut.append( sPath, iPos, iNext-iPos ); } - + sOut.swap( sPath ); } @@ -2436,6 +2436,7 @@ LuaFunction( URLEncode, URLEncode( SArg(1) ) ); LuaFunction( PrettyPercent, PrettyPercent( FArg(1), FArg(2) ) ); //LuaFunction( IsHexVal, IsHexVal( SArg(1) ) ); 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) From 72cd6edf5c93ce5db8c198158d99fde1a3aaad02 Mon Sep 17 00:00:00 2001 From: quietly-turning Date: Wed, 9 Sep 2020 15:37:01 -0400 Subject: [PATCH 2/2] 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(). --- Docs/Luadoc/Lua.xml | 3 ++ Docs/Luadoc/LuaDocumentation.xml | 12 ++++++++ extern/CMakeProject-tomcrypt.cmake | 1 + src/CryptManager.cpp | 47 ++++++++++++++++++++++++++++++ src/CryptManager.h | 14 +++++---- 5 files changed, 71 insertions(+), 6 deletions(-) diff --git a/Docs/Luadoc/Lua.xml b/Docs/Luadoc/Lua.xml index 3e3415f4ef..6b0cde4ffb 100644 --- a/Docs/Luadoc/Lua.xml +++ b/Docs/Luadoc/Lua.xml @@ -12,6 +12,7 @@ + @@ -757,6 +758,8 @@ + + diff --git a/Docs/Luadoc/LuaDocumentation.xml b/Docs/Luadoc/LuaDocumentation.xml index 46e085c629..acd8167659 100644 --- a/Docs/Luadoc/LuaDocumentation.xml +++ b/Docs/Luadoc/LuaDocumentation.xml @@ -74,6 +74,10 @@ save yourself some time, copy this for undocumented things: Returns the last named component of path. For example, passing in "/path/to/file.txt" will return "file.txt", while "/path/to/some/directory/" will return "directory". + + Converts a binary formatted string to hexadecimal format. + This can be useful in conjunction with 's MD5 and SHA functions. + [02 Colors.lua] Returns the color that results from multiplying c's R, G, and B values by fBoost. @@ -2510,6 +2514,14 @@ Def.BitmapText{ Returns the SHA-1 hash for s. + + Returns the SHA-256 hash for the file at sPath as a binary formatted string.
+ You can use to convert to hexadecimal format. +
+ + Returns the SHA-256 hash for s as a binary formatted string.
+ You can use to convert to hexadecimal format. +
diff --git a/extern/CMakeProject-tomcrypt.cmake b/extern/CMakeProject-tomcrypt.cmake index 1cc1af4711..e992275204 100644 --- a/extern/CMakeProject-tomcrypt.cmake +++ b/extern/CMakeProject-tomcrypt.cmake @@ -823,6 +823,7 @@ else() sm_add_compile_definition("tomcrypt" LTC_DEVRANDOM) # 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_MD5) diff --git a/src/CryptManager.cpp b/src/CryptManager.cpp index fa882d51bd..71e0929d28 100644 --- a/src/CryptManager.cpp +++ b/src/CryptManager.cpp @@ -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 ); } }; diff --git a/src/CryptManager.h b/src/CryptManager.h index ee897346d2..31c646b714 100644 --- a/src/CryptManager.h +++ b/src/CryptManager.h @@ -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