summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-04-25 03:23:37 +0430
committerAndreas Kling <kling@serenityos.org>2020-05-02 12:24:10 +0200
commite997661e26d404132308addf21f499d35b787a1a (patch)
tree5ec328976516a93cebd2d3b2ba4114556f85a16e /Userland
parente015ffd5f0812b102fc4c6435a42229f4fe838c5 (diff)
downloadserenity-e997661e26d404132308addf21f499d35b787a1a.zip
LibCrypto: Implement SHA1 Hash Function
Diffstat (limited to 'Userland')
-rw-r--r--Userland/test-crypto.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/Userland/test-crypto.cpp b/Userland/test-crypto.cpp
index 5fc18b7e02..f148527d42 100644
--- a/Userland/test-crypto.cpp
+++ b/Userland/test-crypto.cpp
@@ -6,6 +6,7 @@
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibCrypto/Cipher/AES.h>
#include <LibCrypto/Hash/MD5.h>
+#include <LibCrypto/Hash/SHA1.h>
#include <LibCrypto/Hash/SHA2.h>
#include <LibCrypto/PK/RSA.h>
#include <LibLine/Editor.h>
@@ -32,6 +33,7 @@ int aes_cbc_tests();
// Hash
int md5_tests();
+int sha1_tests();
int sha256_tests();
int sha512_tests();
@@ -141,6 +143,15 @@ void hmac_md5(const char* message, size_t len)
print_buffer(ByteBuffer::wrap(mac.data, hmac.DigestSize), -1);
}
+void sha1(const char* message, size_t len)
+{
+ auto digest = Crypto::Hash::SHA1::hash((const u8*)message, len);
+ if (binary)
+ printf("%.*s", (int)Crypto::Hash::SHA1::digest_size(), digest.data);
+ else
+ print_buffer(ByteBuffer::wrap(digest.data, Crypto::Hash::SHA1::digest_size()), -1);
+}
+
void sha256(const char* message, size_t len)
{
auto digest = Crypto::Hash::SHA256::hash((const u8*)message, len);
@@ -219,6 +230,11 @@ auto main(int argc, char** argv) -> int
return md5_tests();
return run(md5);
}
+ if (suite_sv == "SHA1") {
+ if (run_tests)
+ return sha1_tests();
+ return run(sha1);
+ }
if (suite_sv == "SHA256") {
if (run_tests)
return sha256_tests();
@@ -316,6 +332,9 @@ void md5_test_name();
void md5_test_hash();
void md5_test_consecutive_updates();
+void sha1_test_name();
+void sha1_test_hash();
+
void sha256_test_name();
void sha256_test_hash();
@@ -677,6 +696,40 @@ void hmac_md5_test_process()
}
}
+int sha1_tests()
+{
+ sha1_test_name();
+ sha1_test_hash();
+ return 0;
+}
+
+void sha1_test_name()
+{
+ I_TEST((SHA1 class name));
+ Crypto::Hash::SHA1 sha;
+ if (sha.class_name() != "SHA1") {
+ FAIL(Invalid class name);
+ printf("%s\n", sha.class_name().characters());
+ } else
+ PASS;
+}
+
+void sha1_test_hash()
+{
+ {
+ I_TEST((SHA256 Hashing | ""));
+ u8 result[] {
+ 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
+ };
+ auto digest = Crypto::Hash::SHA1::hash("");
+ if (memcmp(result, digest.data, Crypto::Hash::SHA1::digest_size()) != 0) {
+ FAIL(Invalid hash);
+ print_buffer(ByteBuffer::wrap(digest.data, Crypto::Hash::SHA1::digest_size()), -1);
+ } else
+ PASS;
+ }
+}
+
int sha256_tests()
{
sha256_test_name();