/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
final int schedule[] = desSetKey(key); final int out[] = body(schedule, eSwap0, eSwap1); final byte b[] = new byte[9]; intToFourBytes(out[0], b, 0); intToFourBytes(out[1], b, 4); b[8] = 0; int i = 2;
private static int fourBytesToInt(final byte b[], int offset) { int value = byteToUnsigned(b[offset++]); value |= byteToUnsigned(b[offset++]) << 8; value |= byteToUnsigned(b[offset++]) << 16; value |= byteToUnsigned(b[offset++]) << 24; return value; }
for (int j = 0; j < 25; j++) { for (int i = 0; i < 32; i += 4) { left = dEncrypt(left, right, i, eSwap0, eSwap1, schedule); right = dEncrypt(right, left, i + 2, eSwap0, eSwap1, schedule); left = t >>> 1 | t << 31; final int results[] = new int[2]; permOp(right, left, 1, 0x55555555, results); right = results[0]; left = results[1]; permOp(left, right, 8, 0xff00ff, results); left = results[0]; right = results[1]; permOp(right, left, 2, 0x33333333, results); right = results[0]; left = results[1]; permOp(left, right, 16, 65535, results); left = results[0]; right = results[1]; permOp(right, left, 4, 0xf0f0f0f, results); right = results[0]; left = results[1];
private static int[] desSetKey(final byte key[]) { final int schedule[] = new int[32]; int c = fourBytesToInt(key, 0); int d = fourBytesToInt(key, 4); final int results[] = new int[2]; permOp(d, c, 4, 0xf0f0f0f, results); d = results[0]; c = results[1]; c = hPermOp(c, -2, 0xcccc0000); d = hPermOp(d, -2, 0xcccc0000); permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1]; permOp(c, d, 8, 0xff00ff, results); c = results[0]; d = results[1]; permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1];
private static int[] desSetKey(final byte key[]) { final int schedule[] = new int[32]; int c = fourBytesToInt(key, 0); int d = fourBytesToInt(key, 4); final int results[] = new int[2]; permOp(d, c, 4, 0xf0f0f0f, results); d = results[0]; c = results[1]; c = hPermOp(c, -2, 0xcccc0000); d = hPermOp(d, -2, 0xcccc0000); permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1]; permOp(c, d, 8, 0xff00ff, results); c = results[0]; d = results[1]; permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1];
for (int j = 0; j < 25; j++) { for (int i = 0; i < 32; i += 4) { left = dEncrypt(left, right, i, eSwap0, eSwap1, schedule); right = dEncrypt(right, left, i + 2, eSwap0, eSwap1, schedule); left = t >>> 1 | t << 31; final int results[] = new int[2]; permOp(right, left, 1, 0x55555555, results); right = results[0]; left = results[1]; permOp(left, right, 8, 0xff00ff, results); left = results[0]; right = results[1]; permOp(right, left, 2, 0x33333333, results); right = results[0]; left = results[1]; permOp(left, right, 16, 65535, results); left = results[0]; right = results[1]; permOp(right, left, 4, 0xf0f0f0f, results); right = results[0]; left = results[1];
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one is used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final String original) { return crypt(original.getBytes(Charsets.UTF_8)); }
private static int[] desSetKey(final byte key[]) { final int schedule[] = new int[32]; int c = fourBytesToInt(key, 0); int d = fourBytesToInt(key, 4); final int results[] = new int[2]; permOp(d, c, 4, 0xf0f0f0f, results); d = results[0]; c = results[1]; c = hPermOp(c, -2, 0xcccc0000); d = hPermOp(d, -2, 0xcccc0000); permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1]; permOp(c, d, 8, 0xff00ff, results); c = results[0]; d = results[1]; permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1];
final int schedule[] = desSetKey(key); final int out[] = body(schedule, eSwap0, eSwap1); final byte b[] = new byte[9]; intToFourBytes(out[0], b, 0); intToFourBytes(out[1], b, 4); b[8] = 0; int i = 2;
for (int j = 0; j < 25; j++) { for (int i = 0; i < 32; i += 4) { left = dEncrypt(left, right, i, eSwap0, eSwap1, schedule); right = dEncrypt(right, left, i + 2, eSwap0, eSwap1, schedule); left = t >>> 1 | t << 31; final int results[] = new int[2]; permOp(right, left, 1, 0x55555555, results); right = results[0]; left = results[1]; permOp(left, right, 8, 0xff00ff, results); left = results[0]; right = results[1]; permOp(right, left, 2, 0x33333333, results); right = results[0]; left = results[1]; permOp(left, right, 16, 65535, results); left = results[0]; right = results[1]; permOp(right, left, 4, 0xf0f0f0f, results); right = results[0]; left = results[1];
private static int fourBytesToInt(final byte b[], int offset) { int value = byteToUnsigned(b[offset++]); value |= byteToUnsigned(b[offset++]) << 8; value |= byteToUnsigned(b[offset++]) << 16; value |= byteToUnsigned(b[offset++]) << 24; return value; }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * * @param original * plaintext password * @param salt * a two character string drawn from [a-zA-Z0-9./] or null for a random one * @return a 13 character string starting with the salt string * @throws IllegalArgumentException * if the salt does not match the allowed pattern */ public static String crypt(final String original, final String salt) { return crypt(original.getBytes(Charsets.UTF_8), salt); }
private static int[] desSetKey(final byte key[]) { final int schedule[] = new int[32]; int c = fourBytesToInt(key, 0); int d = fourBytesToInt(key, 4); final int results[] = new int[2]; permOp(d, c, 4, 0xf0f0f0f, results); d = results[0]; c = results[1]; c = hPermOp(c, -2, 0xcccc0000); d = hPermOp(d, -2, 0xcccc0000); permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1]; permOp(c, d, 8, 0xff00ff, results); c = results[0]; d = results[1]; permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1];
final int schedule[] = desSetKey(key); final int out[] = body(schedule, eSwap0, eSwap1); final byte b[] = new byte[9]; intToFourBytes(out[0], b, 0); intToFourBytes(out[1], b, 4); b[8] = 0; int i = 2;
for (int j = 0; j < 25; j++) { for (int i = 0; i < 32; i += 4) { left = dEncrypt(left, right, i, eSwap0, eSwap1, schedule); right = dEncrypt(right, left, i + 2, eSwap0, eSwap1, schedule); left = t >>> 1 | t << 31; final int results[] = new int[2]; permOp(right, left, 1, 0x55555555, results); right = results[0]; left = results[1]; permOp(left, right, 8, 0xff00ff, results); left = results[0]; right = results[1]; permOp(right, left, 2, 0x33333333, results); right = results[0]; left = results[1]; permOp(left, right, 16, 65535, results); left = results[0]; right = results[1]; permOp(right, left, 4, 0xf0f0f0f, results); right = results[0]; left = results[1];
private static int fourBytesToInt(final byte b[], int offset) { int value = byteToUnsigned(b[offset++]); value |= byteToUnsigned(b[offset++]) << 8; value |= byteToUnsigned(b[offset++]) << 16; value |= byteToUnsigned(b[offset++]) << 24; return value; }
/** * Unimplemented "$foo$" salt prefixes would be threated as UnixCrypt salt. */ @Test(expected = IllegalArgumentException.class) public void testUnicCryptInvalidSalt() { UnixCrypt.crypt("secret", "$a"); }
private static int[] desSetKey(final byte key[]) { final int schedule[] = new int[32]; int c = fourBytesToInt(key, 0); int d = fourBytesToInt(key, 4); final int results[] = new int[2]; permOp(d, c, 4, 0xf0f0f0f, results); d = results[0]; c = results[1]; c = hPermOp(c, -2, 0xcccc0000); d = hPermOp(d, -2, 0xcccc0000); permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1]; permOp(c, d, 8, 0xff00ff, results); c = results[0]; d = results[1]; permOp(d, c, 1, 0x55555555, results); d = results[0]; c = results[1];
final int schedule[] = desSetKey(key); final int out[] = body(schedule, eSwap0, eSwap1); final byte b[] = new byte[9]; intToFourBytes(out[0], b, 0); intToFourBytes(out[1], b, 4); b[8] = 0; int i = 2;