|
| 1 | +/* |
| 2 | + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2025 MinIO, Inc. |
| 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 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.minio; |
| 18 | + |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.nio.ByteOrder; |
| 21 | + |
| 22 | +/** Collection of checksum algorithms. */ |
| 23 | +public class Checksum { |
| 24 | + /** Checksum Algorithm. */ |
| 25 | + public static enum Algorithm { |
| 26 | + CRC32, |
| 27 | + CRC32C, |
| 28 | + CRC64NVME, |
| 29 | + SHA1, |
| 30 | + SHA256; |
| 31 | + } |
| 32 | + |
| 33 | + /** CRC32 checksum is an alias of java.util.zip.CRC32. */ |
| 34 | + public static class CRC32 extends java.util.zip.CRC32 {} |
| 35 | + |
| 36 | + // { |
| 37 | + // CRC32 hasher = new CRC32(); |
| 38 | + // hasher.update(value); |
| 39 | + // System.out.println("crc32: " + hasher.getValue()); |
| 40 | + // } |
| 41 | + // |
| 42 | + // { |
| 43 | + // CRC32C hasher = new CRC32C(); |
| 44 | + // hasher.update(value); |
| 45 | + // System.out.println("crc32c: " + hasher.getValue()); |
| 46 | + // } |
| 47 | + // |
| 48 | + // { |
| 49 | + // CRC64NVME hasher = new CRC64NVME(); |
| 50 | + // hasher.update(value); |
| 51 | + // System.out.println("crc64nvme: " + hasher.getValue()); |
| 52 | + // } |
| 53 | + // |
| 54 | + // { |
| 55 | + // MessageDigest hasher = MessageDigest.getInstance("SHA-1"); |
| 56 | + // hasher.update(value); |
| 57 | + // System.out.println("sha-1: " + Arrays.toString(toPositiveValues(hasher.digest()))); |
| 58 | + // } |
| 59 | + // |
| 60 | + // { |
| 61 | + // MessageDigest hasher = MessageDigest.getInstance("SHA-256"); |
| 62 | + // hasher.update(value); |
| 63 | + // System.out.println("sha-256: " + Arrays.toString(toPositiveValues(hasher.digest()))); |
| 64 | + // } |
| 65 | + |
| 66 | + /** CRC32C checksum. */ |
| 67 | + public static class CRC32C implements java.util.zip.Checksum { |
| 68 | + private static final int[] CRC32C_TABLE = new int[256]; |
| 69 | + private int crc = 0xFFFFFFFF; |
| 70 | + |
| 71 | + static { |
| 72 | + for (int i = 0; i < 256; i++) { |
| 73 | + int crc = i; |
| 74 | + for (int j = 0; j < 8; j++) { |
| 75 | + crc = (crc >>> 1) ^ ((crc & 1) != 0 ? 0x82F63B78 : 0); |
| 76 | + } |
| 77 | + CRC32C_TABLE[i] = crc; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void update(int b) { |
| 83 | + crc = CRC32C_TABLE[(crc ^ b) & 0xFF] ^ (crc >>> 8); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void update(byte[] b, int off, int len) { |
| 88 | + for (int i = off; i < off + len; i++) { |
| 89 | + update(b[i]); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public long getValue() { |
| 95 | + return (crc ^ 0xFFFFFFFFL) & 0xFFFFFFFFL; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void reset() { |
| 100 | + crc = 0xFFFFFFFF; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** CRC64NVME checksum logic copied from https://github.com/minio/crc64nvme. */ |
| 105 | + public static class CRC64NVME implements java.util.zip.Checksum { |
| 106 | + private static final long[] CRC64_TABLE = new long[256]; |
| 107 | + private static final long[][] SLICING8_TABLE_NVME = new long[8][256]; |
| 108 | + |
| 109 | + static { |
| 110 | + long polynomial = 0x9A6C9329AC4BC9B5L; |
| 111 | + for (int i = 0; i < 256; i++) { |
| 112 | + long crc = i; |
| 113 | + for (int j = 0; j < 8; j++) { |
| 114 | + if ((crc & 1) == 1) { |
| 115 | + crc = (crc >>> 1) ^ polynomial; |
| 116 | + } else { |
| 117 | + crc >>>= 1; |
| 118 | + } |
| 119 | + } |
| 120 | + CRC64_TABLE[i] = crc; |
| 121 | + } |
| 122 | + |
| 123 | + SLICING8_TABLE_NVME[0] = CRC64_TABLE; |
| 124 | + for (int i = 0; i < 256; i++) { |
| 125 | + long crc = CRC64_TABLE[i]; |
| 126 | + for (int j = 1; j < 8; j++) { |
| 127 | + crc = CRC64_TABLE[(int) crc & 0xFF] ^ (crc >>> 8); |
| 128 | + SLICING8_TABLE_NVME[j][i] = crc; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private long crc = 0; |
| 134 | + |
| 135 | + public CRC64NVME() {} |
| 136 | + |
| 137 | + @Override |
| 138 | + public void update(byte[] p, int off, int len) { |
| 139 | + ByteBuffer byteBuffer = ByteBuffer.wrap(p, off, len); |
| 140 | + byteBuffer.order(ByteOrder.LITTLE_ENDIAN); |
| 141 | + int offset = byteBuffer.position(); |
| 142 | + |
| 143 | + crc = ~crc; |
| 144 | + while (p.length >= 64 && (p.length - offset) > 8) { |
| 145 | + long value = byteBuffer.getLong(); |
| 146 | + crc ^= value; |
| 147 | + crc = |
| 148 | + SLICING8_TABLE_NVME[7][(int) (crc & 0xFF)] |
| 149 | + ^ SLICING8_TABLE_NVME[6][(int) ((crc >>> 8) & 0xFF)] |
| 150 | + ^ SLICING8_TABLE_NVME[5][(int) ((crc >>> 16) & 0xFF)] |
| 151 | + ^ SLICING8_TABLE_NVME[4][(int) ((crc >>> 24) & 0xFF)] |
| 152 | + ^ SLICING8_TABLE_NVME[3][(int) ((crc >>> 32) & 0xFF)] |
| 153 | + ^ SLICING8_TABLE_NVME[2][(int) ((crc >>> 40) & 0xFF)] |
| 154 | + ^ SLICING8_TABLE_NVME[1][(int) ((crc >>> 48) & 0xFF)] |
| 155 | + ^ SLICING8_TABLE_NVME[0][(int) (crc >>> 56)]; |
| 156 | + offset = byteBuffer.position(); |
| 157 | + } |
| 158 | + |
| 159 | + for (; offset < len; offset++) { |
| 160 | + crc = CRC64_TABLE[(int) ((crc ^ (long) p[offset]) & 0xFF)] ^ (crc >>> 8); |
| 161 | + } |
| 162 | + |
| 163 | + crc = ~crc; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void update(byte[] p) { |
| 168 | + update(p, 0, p.length); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void update(int b) { |
| 173 | + update(new byte[] {(byte) b}); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public long getValue() { |
| 178 | + return crc; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public void reset() { |
| 183 | + crc = 0; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public String toString() { |
| 188 | + return String.format("CRC64NVME{value=%016x}", crc); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments