|
13 | 13 | package elide.runtime.node |
14 | 14 |
|
15 | 15 | import org.graalvm.polyglot.Value |
| 16 | +import org.junit.jupiter.api.Assertions.assertArrayEquals |
16 | 17 | import org.junit.jupiter.api.assertThrows |
| 18 | +import java.nio.charset.Charset |
| 19 | +import java.security.MessageDigest |
17 | 20 | import kotlin.test.Test |
18 | 21 | import kotlin.test.assertEquals |
19 | 22 | import kotlin.test.assertIs |
@@ -786,4 +789,97 @@ import elide.testing.annotations.TestCase |
786 | 789 | ); |
787 | 790 | """ |
788 | 791 | } |
| 792 | + |
| 793 | + @Test fun `digest should support base64, buffer, hex, and latin1 encodings when specified`() = conforms { |
| 794 | + // Base64 encoding |
| 795 | + val base64 = crypto.provide().createHash("sha256") |
| 796 | + base64.update("hello world") |
| 797 | + val base64Digest = base64.digest("base64") |
| 798 | + assertEquals( |
| 799 | + "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=", |
| 800 | + base64Digest, |
| 801 | + "Base64 digest should match expected value" |
| 802 | + ) |
| 803 | + |
| 804 | + // Buffer encoding |
| 805 | + val buffer = crypto.provide().createHash("sha256") |
| 806 | + buffer.update("hello world") |
| 807 | + val bufferDigest = buffer.digest("buffer") |
| 808 | + assertIs<NodeHostBuffer>(bufferDigest, "Buffer digest should be a NodeHostBuffer") |
| 809 | + assertEquals( |
| 810 | + "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", |
| 811 | + bufferDigest.toString("hex", null, null), |
| 812 | + "Buffer digest should match expected value" |
| 813 | + ) |
| 814 | + |
| 815 | + // Hex encoding |
| 816 | + val hex = crypto.provide().createHash("sha256") |
| 817 | + hex.update("hello world") |
| 818 | + |
| 819 | + val hexDigest = hex.digest("hex") |
| 820 | + assertEquals( |
| 821 | + "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", |
| 822 | + hexDigest, |
| 823 | + "Hex digest should match expected value" |
| 824 | + ) |
| 825 | + |
| 826 | + // Latin1 encoding |
| 827 | + val latin1 = crypto.provide().createHash("sha256") |
| 828 | + latin1.update("hello world") |
| 829 | + val latin1String = latin1.digest("latin1") |
| 830 | + |
| 831 | + assertIs<String>(latin1String, "Latin1 digest should be a String") |
| 832 | + val latin1Bytes = latin1String.toByteArray(Charset.forName("ISO-8859-1")) |
| 833 | + val expectedLatin1Bytes = MessageDigest.getInstance("SHA-256") |
| 834 | + .digest("hello world".toByteArray(Charsets.UTF_8)) |
| 835 | + |
| 836 | + assertArrayEquals(expectedLatin1Bytes, latin1Bytes) |
| 837 | + }.guest { |
| 838 | + //language=javascript |
| 839 | + """ |
| 840 | + const crypto = require("crypto") |
| 841 | + const assert = require("assert") |
| 842 | + const { Buffer } = require("buffer"); |
| 843 | + |
| 844 | + // Base64 encoding |
| 845 | + const base64Digest = crypto.createHash("sha256").update("hello world").digest("base64"); |
| 846 | +
|
| 847 | + assert.equal( |
| 848 | + "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=", |
| 849 | + base64Digest, |
| 850 | + "Base64 digest should match expected value" |
| 851 | + ); |
| 852 | +
|
| 853 | + // Buffer encoding |
| 854 | + const bufferDigest = crypto.createHash("sha256").update("hello world").digest("buffer") |
| 855 | + |
| 856 | + assert.equal(Buffer.isBuffer(bufferDigest), true, "Default digest output should be a Buffer"); |
| 857 | + |
| 858 | + const expectedBufferHex = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"; |
| 859 | + const actualBufferHex = bufferDigest.toString("hex"); |
| 860 | + |
| 861 | + assert.equal( |
| 862 | + expectedBufferHex, |
| 863 | + actualBufferHex, |
| 864 | + "Buffer digest should match expected value" |
| 865 | + ); |
| 866 | +
|
| 867 | + // Hex encoding |
| 868 | + const hexDigest = crypto.createHash("sha256").update("hello world").digest("hex"); |
| 869 | +
|
| 870 | + assert.equal( |
| 871 | + "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", |
| 872 | + hexDigest, |
| 873 | + "Hex digest should match expected value" |
| 874 | + ); |
| 875 | + |
| 876 | + // Latin1 encoding |
| 877 | + const latin1Digest = crypto.createHash("sha256").update("hello world").digest("latin1"); |
| 878 | + |
| 879 | + const actualLatin1ToHex = Buffer.from(latin1Digest, "latin1").toString("hex"); |
| 880 | + const expectedLatin1ToHex = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"; |
| 881 | + |
| 882 | + assert.strict(expectedLatin1ToHex, actualLatin1ToHex, "Latin1 digest bytes should match expected value"); |
| 883 | + """ |
| 884 | + } |
789 | 885 | } |
0 commit comments