SM3杂凑算法实现 @author SeanDragon
| 11 | * @author SeanDragon |
| 12 | */ |
| 13 | public class SM3 { |
| 14 | |
| 15 | private static final String ivHexStr = "7380166f 4914b2b9 172442d7 da8a0600 a96f30bc 163138aa e38dee4d b0fb0e4e"; |
| 16 | private static final BigInteger IV = new BigInteger(ivHexStr.replaceAll(" ", |
| 17 | ""), 16); |
| 18 | private static final Integer Tj15 = Integer.valueOf("79cc4519", 16); |
| 19 | private static final Integer Tj63 = Integer.valueOf("7a879d8a", 16); |
| 20 | private static final byte[] FirstPadding = {(byte) 0x80}; |
| 21 | private static final byte[] ZeroPadding = {(byte) 0x00}; |
| 22 | private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', |
| 23 | '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 24 | |
| 25 | private static int T(int j) { |
| 26 | if (j >= 0 && j <= 15) { |
| 27 | return Tj15; |
| 28 | } else if (j >= 16 && j <= 63) { |
| 29 | return Tj63; |
| 30 | } else { |
| 31 | throw new RuntimeException("data invalid"); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private static Integer FF(Integer x, Integer y, Integer z, int j) { |
| 36 | if (j >= 0 && j <= 15) { |
| 37 | return x ^ y ^ z; |
| 38 | } else if (j >= 16 && j <= 63) { |
| 39 | return (x & y) |
| 40 | | (x & z) |
| 41 | | (y & z); |
| 42 | } else { |
| 43 | throw new RuntimeException("data invalid"); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private static Integer GG(Integer x, Integer y, Integer z, int j) { |
| 48 | if (j >= 0 && j <= 15) { |
| 49 | return x ^ y ^ z; |
| 50 | } else if (j >= 16 && j <= 63) { |
| 51 | return (x & y) | (~x & z); |
| 52 | } else { |
| 53 | throw new RuntimeException("data invalid"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private static Integer P0(Integer x) { |
| 58 | return x |
| 59 | ^ Integer.rotateLeft(x, 9) |
| 60 | ^ Integer.rotateLeft(x, 17); |
| 61 | } |
| 62 | |
| 63 | private static Integer P1(Integer x) { |
| 64 | return x |
| 65 | ^ Integer.rotateLeft(x, 15) |
| 66 | ^ Integer.rotateLeft(x, 23); |
| 67 | } |
| 68 | |
| 69 | private static byte[] padding(byte[] source) throws IOException { |
| 70 | long l = source.length * 8; |
nothing calls this directly
no outgoing calls
no test coverage detected