/** * Take a byte array and return the hex representation of its SHA-1 using the standard state * values */ public static String encode(short[] shorts, boolean calculatePadding) { // These are the default values int[] stateValues = extractState("67452301EFCDAB8998BADCFE10325476C3D2E1F0"); checkAssertion(stateValues[0] == 1732584193); checkAssertion(stateValues[1] == -271733879); checkAssertion(stateValues[2] == -1732584194); checkAssertion(stateValues[3] == 271733878); checkAssertion(stateValues[4] == -1009589776); return encode(shorts, stateValues, calculatePadding); }
/** * Extracts the state of a SHA-1 hash from a hex string containing 5 32-bit words * * @param state * @return */ private static int[] extractState(String state) { // Make sure the state isn't NULL checkAssertion(state != null); // Make sure the state is the correct length (40 characters) checkAssertion(state.length() == 40); // Extract the 5 32-bit words int H0 = (int) Long.parseLong(state.substring(0, 8), 16); int H1 = (int) Long.parseLong(state.substring(8, 16), 16); int H2 = (int) Long.parseLong(state.substring(16, 24), 16); int H3 = (int) Long.parseLong(state.substring(24, 32), 16); int H4 = (int) Long.parseLong(state.substring(32, 40), 16); // Put the 5 32-bit words into an array int[] stateValues = new int[5]; stateValues[0] = H0; stateValues[1] = H1; stateValues[2] = H2; stateValues[3] = H3; stateValues[4] = H4; return stateValues; }