|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 OnixByte. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.devkit.utils; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | + |
| 26 | +class HashUtilTest { |
| 27 | + |
| 28 | + // Test MD2 hashing with explicit charset and default charset |
| 29 | + @Test |
| 30 | + void testMd2() { |
| 31 | + String input = "test"; |
| 32 | + // Known MD2 hash of "test" with UTF-8 |
| 33 | + String expectedHash = "dd34716876364a02d0195e2fb9ae2d1b"; |
| 34 | + assertEquals(expectedHash, HashUtil.md2(input, StandardCharsets.UTF_8)); |
| 35 | + assertEquals(expectedHash, HashUtil.md2(input)); |
| 36 | + // Test null charset fallback to UTF-8 |
| 37 | + assertEquals(expectedHash, HashUtil.md2(input, null)); |
| 38 | + } |
| 39 | + |
| 40 | + // Test MD5 hashing with explicit charset and default charset |
| 41 | + @Test |
| 42 | + void testMd5() { |
| 43 | + String input = "test"; |
| 44 | + // Known MD5 hash of "test" |
| 45 | + String expectedHash = "098f6bcd4621d373cade4e832627b4f6"; |
| 46 | + assertEquals(expectedHash, HashUtil.md5(input, StandardCharsets.UTF_8)); |
| 47 | + assertEquals(expectedHash, HashUtil.md5(input)); |
| 48 | + assertEquals(expectedHash, HashUtil.md5(input, null)); |
| 49 | + } |
| 50 | + |
| 51 | + // Test SHA-1 hashing with explicit charset and default charset |
| 52 | + @Test |
| 53 | + void testSha1() { |
| 54 | + String input = "test"; |
| 55 | + // Known SHA-1 hash of "test" |
| 56 | + String expectedHash = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"; |
| 57 | + assertEquals(expectedHash, HashUtil.sha1(input, StandardCharsets.UTF_8)); |
| 58 | + assertEquals(expectedHash, HashUtil.sha1(input)); |
| 59 | + assertEquals(expectedHash, HashUtil.sha1(input, null)); |
| 60 | + } |
| 61 | + |
| 62 | + // Test SHA-224 hashing with explicit charset and default charset |
| 63 | + @Test |
| 64 | + void testSha224() { |
| 65 | + String input = "test"; |
| 66 | + // Known SHA-224 hash of "test" |
| 67 | + String expectedHash = "90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809"; |
| 68 | + assertEquals(expectedHash, HashUtil.sha224(input, StandardCharsets.UTF_8)); |
| 69 | + assertEquals(expectedHash, HashUtil.sha224(input)); |
| 70 | + assertEquals(expectedHash, HashUtil.sha224(input, null)); |
| 71 | + } |
| 72 | + |
| 73 | + // Test SHA-256 hashing with explicit charset and default charset |
| 74 | + @Test |
| 75 | + void testSha256() { |
| 76 | + String input = "test"; |
| 77 | + // Known SHA-256 hash of "test" |
| 78 | + String expectedHash = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"; |
| 79 | + assertEquals(expectedHash, HashUtil.sha256(input, StandardCharsets.UTF_8)); |
| 80 | + assertEquals(expectedHash, HashUtil.sha256(input)); |
| 81 | + assertEquals(expectedHash, HashUtil.sha256(input, null)); |
| 82 | + } |
| 83 | + |
| 84 | + // Test SHA-384 hashing with explicit charset and default charset |
| 85 | + @Test |
| 86 | + void testSha384() { |
| 87 | + String input = "test"; |
| 88 | + // Known SHA-384 hash of "test" |
| 89 | + String expectedHash = "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9"; |
| 90 | + assertEquals(expectedHash, HashUtil.sha384(input, StandardCharsets.UTF_8)); |
| 91 | + assertEquals(expectedHash, HashUtil.sha384(input)); |
| 92 | + assertEquals(expectedHash, HashUtil.sha384(input, null)); |
| 93 | + } |
| 94 | + |
| 95 | + // Test SHA-512 hashing with explicit charset and default charset |
| 96 | + @Test |
| 97 | + void testSha512() { |
| 98 | + String input = "test"; |
| 99 | + // Known SHA-512 hash of "test" |
| 100 | + String expectedHash = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; |
| 101 | + // remove all whitespace in expected to match format generated |
| 102 | + expectedHash = expectedHash.replaceAll("\\s+", ""); |
| 103 | + assertEquals(expectedHash, HashUtil.sha512(input, StandardCharsets.UTF_8)); |
| 104 | + assertEquals(expectedHash, HashUtil.sha512(input)); |
| 105 | + assertEquals(expectedHash, HashUtil.sha512(input, null)); |
| 106 | + } |
| 107 | + |
| 108 | + // Test empty string input |
| 109 | + @Test |
| 110 | + void testEmptyString() { |
| 111 | + String input = ""; |
| 112 | + // MD5 hash of empty string |
| 113 | + String expectedMd5 = "d41d8cd98f00b204e9800998ecf8427e"; |
| 114 | + assertEquals(expectedMd5, HashUtil.md5(input)); |
| 115 | + // SHA-256 hash of empty string |
| 116 | + String expectedSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; |
| 117 | + assertEquals(expectedSha256, HashUtil.sha256(input)); |
| 118 | + } |
| 119 | + |
| 120 | + // Test null charset fallback for one algorithm as a sample |
| 121 | + @Test |
| 122 | + void testNullCharsetFallsBackToUtf8() { |
| 123 | + String input = "abc"; |
| 124 | + String hashWithNull = HashUtil.md5(input, null); |
| 125 | + String hashWithUtf8 = HashUtil.md5(input, StandardCharsets.UTF_8); |
| 126 | + assertEquals(hashWithUtf8, hashWithNull); |
| 127 | + } |
| 128 | +} |
0 commit comments