TestCryptoHash.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestCryptoHash.h"
  18. #include "crypto/Crypto.h"
  19. #include "crypto/CryptoHash.h"
  20. #include <QTest>
  21. QTEST_GUILESS_MAIN(TestCryptoHash)
  22. void TestCryptoHash::initTestCase()
  23. {
  24. QVERIFY(Crypto::init());
  25. }
  26. void TestCryptoHash::test()
  27. {
  28. CryptoHash cryptoHash1(CryptoHash::Sha256);
  29. QCOMPARE(cryptoHash1.result(),
  30. QByteArray::fromHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
  31. QByteArray source2 = QString("KeePassX").toLatin1();
  32. QByteArray result2 = CryptoHash::hash(source2, CryptoHash::Sha256);
  33. QCOMPARE(result2, QByteArray::fromHex("0b56e5f65263e747af4a833bd7dd7ad26a64d7a4de7c68e52364893dca0766b4"));
  34. CryptoHash cryptoHash3(CryptoHash::Sha256);
  35. cryptoHash3.addData(QString("KeePa").toLatin1());
  36. cryptoHash3.addData(QString("ssX").toLatin1());
  37. QCOMPARE(cryptoHash3.result(),
  38. QByteArray::fromHex("0b56e5f65263e747af4a833bd7dd7ad26a64d7a4de7c68e52364893dca0766b4"));
  39. CryptoHash cryptoHash2(CryptoHash::Sha512);
  40. QCOMPARE(cryptoHash2.result(),
  41. QByteArray::fromHex("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff831"
  42. "8d2877eec2f63b931bd47417a81a538327af927da3e"));
  43. QByteArray result3 = CryptoHash::hash(source2, CryptoHash::Sha512);
  44. QCOMPARE(result3,
  45. QByteArray::fromHex("0d41b612584ed39ff72944c29494573e40f4bb95283455fae2e0be1e3565aa9f48057d59e6ff"
  46. "d777970e282871c25a549a2763e5b724794f312c97021c42f91d"));
  47. CryptoHash cryptoHash4(CryptoHash::Sha512);
  48. cryptoHash4.addData(QString("KeePa").toLatin1());
  49. cryptoHash4.addData(QString("ssX").toLatin1());
  50. QCOMPARE(cryptoHash4.result(),
  51. QByteArray::fromHex("0d41b612584ed39ff72944c29494573e40f4bb95283455fae2e0be1e3565aa9f48057d59e6ffd777970e2"
  52. "82871c25a549a2763e5b724794f312c97021c42f91d"));
  53. }