Skip to content

Commit 23f4d5e

Browse files
lbartolettinyalldawson
authored andcommittedOct 25, 2019
[FEATURE][needs-docs] Add hash functions
1 parent 150d6ce commit 23f4d5e

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4991,8 +4991,85 @@ static QVariant fcnFileSize( const QVariantList &values, const QgsExpressionCont
49914991
return QFileInfo( file ).size();
49924992
}
49934993

4994+
static QVariant fcnHash( const QString str, const QCryptographicHash::Algorithm algorithm )
4995+
{
4996+
return QString( QCryptographicHash::hash( str.toUtf8(), algorithm ).toHex() );
4997+
}
4998+
4999+
static QVariant fcnHashMd4( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5000+
{
5001+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Md4 );
5002+
}
5003+
5004+
static QVariant fcnHashMd5( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5005+
{
5006+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Md5 );
5007+
}
5008+
5009+
static QVariant fcnHashSha1( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5010+
{
5011+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha1 );
5012+
}
5013+
5014+
static QVariant fcnHashSha224( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5015+
{
5016+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha224 );
5017+
}
5018+
5019+
static QVariant fcnHashSha256( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5020+
{
5021+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha256 );
5022+
}
5023+
5024+
static QVariant fcnHashSha384( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5025+
{
5026+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha384 );
5027+
}
5028+
5029+
static QVariant fcnHashSha512( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5030+
{
5031+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha512 );
5032+
}
49945033

5034+
static QVariant fcnHashSha3_224( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5035+
{
5036+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha3_224 );
5037+
}
5038+
5039+
static QVariant fcnHashSha3_256( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5040+
{
5041+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha3_256 );
5042+
}
5043+
5044+
static QVariant fcnHashSha3_384( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5045+
{
5046+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha3_384 );
5047+
}
49955048

5049+
static QVariant fcnHashSha3_512( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5050+
{
5051+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha3_512 );
5052+
}
5053+
5054+
static QVariant fcnHashKeccak_224( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5055+
{
5056+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Keccak_224 );
5057+
}
5058+
5059+
static QVariant fcnHashKeccak_256( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5060+
{
5061+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Keccak_256 );
5062+
}
5063+
5064+
static QVariant fcnHashKeccak_384( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5065+
{
5066+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Keccak_384 );
5067+
}
5068+
5069+
static QVariant fcnHashKeccak_512( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
5070+
{
5071+
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Keccak_512 );
5072+
}
49965073

49975074
const QList<QgsExpressionFunction *> &QgsExpression::Functions()
49985075
{
@@ -5288,6 +5365,38 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
52885365
<< new QgsStaticExpressionFunction( QStringLiteral( "file_size" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "path" ) ),
52895366
fcnFileSize, QStringLiteral( "Files and Paths" ) )
52905367

5368+
// hash
5369+
<< new QgsStaticExpressionFunction( QStringLiteral( "md4" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5370+
fcnHashMd4, QStringLiteral( "Hash" ) )
5371+
<< new QgsStaticExpressionFunction( QStringLiteral( "md5" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5372+
fcnHashMd5, QStringLiteral( "Hash" ) )
5373+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha1" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5374+
fcnHashSha1, QStringLiteral( "Hash" ) )
5375+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha224" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5376+
fcnHashSha224, QStringLiteral( "Hash" ) )
5377+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha256" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5378+
fcnHashSha256, QStringLiteral( "Hash" ) )
5379+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha384" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5380+
fcnHashSha384, QStringLiteral( "Hash" ) )
5381+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha512" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5382+
fcnHashSha512, QStringLiteral( "Hash" ) )
5383+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha3_224" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5384+
fcnHashSha3_224, QStringLiteral( "Hash" ) )
5385+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha3_256" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5386+
fcnHashSha3_256, QStringLiteral( "Hash" ) )
5387+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha3_384" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5388+
fcnHashSha3_384, QStringLiteral( "Hash" ) )
5389+
<< new QgsStaticExpressionFunction( QStringLiteral( "sha3_512" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5390+
fcnHashSha3_512, QStringLiteral( "Hash" ) )
5391+
<< new QgsStaticExpressionFunction( QStringLiteral( "keccak_224" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5392+
fcnHashKeccak_224, QStringLiteral( "Hash" ) )
5393+
<< new QgsStaticExpressionFunction( QStringLiteral( "keccak_256" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5394+
fcnHashKeccak_256, QStringLiteral( "Hash" ) )
5395+
<< new QgsStaticExpressionFunction( QStringLiteral( "keccak_384" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5396+
fcnHashKeccak_384, QStringLiteral( "Hash" ) )
5397+
<< new QgsStaticExpressionFunction( QStringLiteral( "keccak_512" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
5398+
fcnHashKeccak_512, QStringLiteral( "Hash" ) )
5399+
52915400
// deprecated stuff - hidden from users
52925401
<< new QgsStaticExpressionFunction( QStringLiteral( "$scale" ), QgsExpressionFunction::ParameterList(), fcnMapScale, QStringLiteral( "deprecated" ) );
52935402

‎tests/src/core/testqgsexpression.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,38 @@ class TestQgsExpression: public QObject
14711471
QTest::newRow( "is_file(points.shp)" ) << QStringLiteral( "is_file('%1/points.shp')" ).arg( TEST_DATA_DIR ) << false << QVariant( true );
14721472
QTest::newRow( "is_file(valid)" ) << QStringLiteral( "is_file('%1')" ).arg( TEST_DATA_DIR ) << false << QVariant( false );
14731473

1474+
// hash functions
1475+
QTest::newRow( "md4(NULL)" ) << QStringLiteral( "md4(NULL)" ) << false << QVariant();
1476+
QTest::newRow( "md4('QGIS')" ) << QStringLiteral( "md4('QGIS')" ) << false << QVariant( "c0fc71c241cdebb6e888cbac0e2b68eb" );
1477+
QTest::newRow( "md5(NULL)" ) << QStringLiteral( "md5(NULL)" ) << false << QVariant();
1478+
QTest::newRow( "md5('QGIS')" ) << QStringLiteral( "md5('QGIS')" ) << false << QVariant( "57470aaa9e22adaefac7f5f342f1c6da" );
1479+
QTest::newRow( "sha1(NULL)" ) << QStringLiteral( "sha1(NULL)" ) << false << QVariant();
1480+
QTest::newRow( "sha1('QGIS')" ) << QStringLiteral( "sha1('QGIS')" ) << false << QVariant( "f87cfb2b74cdd5867db913237024e7001e62b114" );
1481+
QTest::newRow( "sha224(NULL)" ) << QStringLiteral( "sha224(NULL)" ) << false << QVariant();
1482+
QTest::newRow( "sha224('QGIS')" ) << QStringLiteral( "sha224('QGIS')" ) << false << QVariant( "4093a619ada631c770f44bc643ead18fb393b93d6a6af1861fcfece0" );
1483+
QTest::newRow( "sha256(NULL)" ) << QStringLiteral( "sha256(NULL)" ) << false << QVariant();
1484+
QTest::newRow( "sha256('QGIS')" ) << QStringLiteral( "sha256('QGIS')" ) << false << QVariant( "eb045cba7a797aaa06ac58830846e40c8e8c780bc0676d3393605fae50c05309" );
1485+
QTest::newRow( "sha384(NULL)" ) << QStringLiteral( "sha384(NULL)" ) << false << QVariant();
1486+
QTest::newRow( "sha384('QGIS')" ) << QStringLiteral( "sha384('QGIS')" ) << false << QVariant( "91c1de038cc3d09fdd512e99f9dd9922efadc39ed21d3922e69a4305cc25506033aee388e554b78714c8734f9cd7e610" );
1487+
QTest::newRow( "sha512(NULL)" ) << QStringLiteral( "sha512(NULL)" ) << false << QVariant();
1488+
QTest::newRow( "sha512('QGIS')" ) << QStringLiteral( "sha512('QGIS')" ) << false << QVariant( "c2c092f2ab743bf8edbeb6d028a745f30fc720408465ed369421f0a4e20fa5e27f0c90ad72d3f1d836eaa5d25cd39897d4cf77e19984668ef58da6e3159f18ac" );
1489+
QTest::newRow( "sha3_224(NULL)" ) << QStringLiteral( "sha3_224(NULL)" ) << false << QVariant();
1490+
QTest::newRow( "sha3_224('QGIS')" ) << QStringLiteral( "sha3_224('QGIS')" ) << false << QVariant( "467f49a5039e7280d5d42fd433e80d203439e338eaabd701f0d6c17d" );
1491+
QTest::newRow( "sha3_256(NULL)" ) << QStringLiteral( "sha3_256(NULL)" ) << false << QVariant();
1492+
QTest::newRow( "sha3_256('QGIS')" ) << QStringLiteral( "sha3_256('QGIS')" ) << false << QVariant( "540f7354b6b8a6e735f2845250f15f4f3ba4f666c55574d9e9354575de0e980f" );
1493+
QTest::newRow( "sha3_384(NULL)" ) << QStringLiteral( "sha3_384(NULL)" ) << false << QVariant();
1494+
QTest::newRow( "sha3_384('QGIS')" ) << QStringLiteral( "sha3_384('QGIS')" ) << false << QVariant( "96052da1e77679e9a65f60d7ead961b287977823144786386eb43647b0901fd8516fa6f1b9d243fb3f28775e6dde6107" );
1495+
QTest::newRow( "sha3_512(NULL)" ) << QStringLiteral( "sha3_512(NULL)" ) << false << QVariant();
1496+
QTest::newRow( "sha3_512('QGIS')" ) << QStringLiteral( "sha3_512('QGIS')" ) << false << QVariant( "900d079dc69761da113980253aa8ac0414a8bd6d09879a916228f8743707c4758051c98445d6b8945ec854ff90655005e02aceb0a2ffc6a0ebf818745d665349" );
1497+
QTest::newRow( "keccak_224(NULL)" ) << QStringLiteral( "keccak_224(NULL)" ) << false << QVariant();
1498+
QTest::newRow( "keccak_224('QGIS')" ) << QStringLiteral( "keccak_224('QGIS')" ) << false << QVariant( "5b0ce6acef8b0a121d4ac4f3eaa8503c799ad4e26a3392d1fb201478" );
1499+
QTest::newRow( "keccak_256(NULL)" ) << QStringLiteral( "keccak_256(NULL)" ) << false << QVariant();
1500+
QTest::newRow( "keccak_256('QGIS')" ) << QStringLiteral( "keccak_256('QGIS')" ) << false << QVariant( "991c520aa6815392de24087f61b2ae0fd56abbfeee4a8ca019c1011d327c577e" );
1501+
QTest::newRow( "keccak_384(NULL)" ) << QStringLiteral( "keccak_384(NULL)" ) << false << QVariant();
1502+
QTest::newRow( "keccak_384('QGIS')" ) << QStringLiteral( "keccak_384('QGIS')" ) << false << QVariant( "c57a3aed9d856fa04e5eeee9b62b6e027cca81ba574116d3cc1f0d48a1ef9e5886ff463ea8d0fac772ee473bf92f810d" );
1503+
QTest::newRow( "keccak_512(NULL)" ) << QStringLiteral( "keccak_512(NULL)" ) << false << QVariant();
1504+
QTest::newRow( "keccak_512('QGIS')" ) << QStringLiteral( "keccak_512('QGIS')" ) << false << QVariant( "6f0f751776b505e317de222508fa5d3ed7099d8f07c74fed54ccee6e7cdc6b89b4a085e309f2ee5210c942bbeb142bdfe48f84f912e0f3f41bdbf47110c2d344" );
1505+
14741506
}
14751507

14761508

0 commit comments

Comments
 (0)
Please sign in to comment.