protected MUX firstAvailableMUX (long maxWait) { do { for (MUX m : mux) if (isUsable(m)) return m; ISOUtil.sleep (1000); } while (System.currentTimeMillis() < maxWait); return null; } protected MUX nextAvailableMUX (int mnumber, long maxWait) {
/** * Bitwise XOR between corresponding byte arrays represented in hex * @param op1 hexstring 1 * @param op2 hexstring 2 * @return an array of length = the smallest between op1 and op2 */ public static String hexor (String op1, String op2) { byte[] xor = xor (hex2byte (op1), hex2byte (op2)); return hexString (xor); }
/** * left pad with '0' * @param s - original string * @param len - desired len * @return zero padded string * @throws ISOException if string's length greater than len */ public static String zeropad(String s, int len) throws ISOException { return padleft(s, len, '0'); }
/** * @param c - a component * @return packed component * @exception ISOException */ public byte[] pack (ISOComponent c) throws ISOException { byte[] b = ISOUtil.bitSet2byte ((BitSet) c.getValue()); return ISOUtil.hexString(b).getBytes(); } /**
@Test public void testHexdump17() throws Throwable { byte[] b = new byte[3]; b[1] = (byte) 31; String result = ISOUtil.hexdump(b, 0, 3); assertEquals("result", "0000 00 1F 00 ..." + lineSep, result); }
@Test public void testProtect14() throws Throwable { String result = ISOUtil.protect("===\u3455w"); assertEquals("result", "===__", result); }
public void dump (PrintStream p, String indent) { p.println (indent +"<"+XMLPackager.ISOFIELD_TAG + " " +XMLPackager.ID_ATTR +"=\"" +fieldNumber +"\" " +"currency=\"" +ISOUtil.zeropad (currencyCode, 3)+"\" " +XMLPackager.TYPE_ATTR +"=\"amount\" " +XMLPackager.VALUE_ATTR+"=\"" + amount.toString() +"\"/>" ); } public void writeExternal (ObjectOutput out) throws IOException {
private byte[] decrypt(byte[] encoded) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException, InvalidAlgorithmParameterException { byte[] iv = new byte[16]; byte[] cryptogram = new byte[encoded.length - iv.length]; System.arraycopy(encoded, 0, iv, 0, iv.length); System.arraycopy(encoded, iv.length, cryptogram, 0, cryptogram.length); final Cipher cipher = Cipher.getInstance(AES); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ISOUtil.xor(iv, SystemSeed.getSeed(iv.length, iv.length)))); return cipher.doFinal(cryptogram); }
@Override public void dump(PrintStream p, String indent) { p.print (indent); p.print ("<tag id='"); p.print (Integer.toHexString(getTag())); p.print ("' value='"); p.print (ISOUtil.hexString(getValue())); p.println ("' />"); } }
/** * prepare long value used as amount for display * (implicit 2 decimals) * @param l value * @param len display len * @return formated field * @exception ISOException */ public static String formatAmount(long l, int len) throws ISOException { String buf = Long.toString(l); if (l < 100) buf = zeropad(buf, 3); StringBuilder s = new StringBuilder(padleft (buf, len-1, ' ') ); s.insert(len-3, '.'); return s.toString(); }
@Test public void testBitSetByteHexInteroperability() throws Throwable { BitSet bs = new BitSet(); bs.set(1); bs.set(63); bs.set(127); bs.set(191); byte[] b = ISOUtil.bitSet2byte(bs); BitSet bs1 = ISOUtil.byte2BitSet(b, 0, 192); BitSet bs2 = ISOUtil.hex2BitSet(ISOUtil.hexString(b).getBytes(), 0, 192); assertEquals("BitSets should be equal", bs1, bs2); }
private String calculateCVD(String accountNo, Key cvk, String expDate, String serviceCode) throws SMException { Key udka = jceHandler.formDESKey(SMAdapter.LENGTH_DES ,Arrays.copyOfRange(cvk.getEncoded(), 0, 8)); byte[] block = ISOUtil.hex2byte( ISOUtil.zeropadRight(accountNo + expDate + serviceCode, 32)); byte[] ba = Arrays.copyOfRange(block, 0, 8); byte[] bb = Arrays.copyOfRange(block, 8,16); //Encrypt ba with udka byte[] bc = jceHandler.encryptData(ba, udka); byte[] bd = ISOUtil.xor(bc, bb); //Encrypt bd Tripple DES byte[] be = jceHandler.encryptData(bd, cvk); return decimalizeVisa(be).substring(0,3); }
@Test public void testThirdBitmapPack() throws Exception { byte[] b = ISOUtil.hex2byte("F23C04800AE00000"+"8000000000000108"+"63BC780000000010"); BitSet bs1 = ISOUtil.byte2BitSet(b, 0, 192); ISOBitMap bmap = new ISOBitMap(-1); bmap.setValue(bs1); IFA_BITMAP ifa = new IFA_BITMAP(24, "BITMAP"); byte[] b1 = ifa.pack(bmap); assertEquals ("Pack should be equal to unpack", ISOUtil.hexString(b), new String(b1)); } }
@Test public void testWellKnownSeeds() throws Throwable { assertEquals("Invalid seed 0", "371B4CF737319AC15BD6B18DE9E8B8537D2D2CC6323B4E728C8A06C654FBBA06", ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(16,32), SystemSeed.getSeed(4064-32)))); assertEquals("Invalid seed 1", "6835BEFB07E7965940A99D46C7FAC87561DCB1FD7BF4932DB5ACC1E0529B1AE891B0368E14D5F9BFFC74E380426C3A7B", ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(0,1024), SystemSeed.getSeed(2048,48)))); assertEquals("Invalid seed 3", "6835BEFB07E7965940A99D46C7FAC87561DCB1FD7BF4932DB5ACC1E0529B1AE891B0368E14D5F9BFFC74E380426C3A7B", ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(0,1024), SystemSeed.getSeed(6144,48)))); assertEquals("Invalid seed 3", ISOUtil.hexString(new byte[8192]), ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(1,8192), SystemSeed.getSeed(4097,16384)))); } }
public void testISOUtilCompatibility() throws Exception { byte[] b=ISOUtil.hex2byte("66656637656236643736323431653438"); AsciiHexInterpreter as=AsciiHexInterpreter.INSTANCE; byte[] n=as.uninterpret(b, 0, 8); byte[] a=ISOUtil.hex2byte("46454637454236443736323431453438"); byte[] n1=as.uninterpret(a, 0, 8); byte[] expected = ISOUtil.hex2byte("fef7eb6d76241e48"); assertEquals (ISOUtil.hexString(expected), ISOUtil.hexString(n)); assertEquals (ISOUtil.hexString(expected), ISOUtil.hexString(n1)); } }
@Override public byte[] pack(Map<String, String> fields) throws ISOException { if (value== null) {// if the hardcoded value is in the constructor , use it. value = fields.get(getName()); } else { fields.put(getName(), value); } if (value==null){ throw new ISOException(String.format("Field [%s]: Unable to pack as field is not set",getName())); } if (value.length() <= size) { if (padLeft){ ISOUtil.padleft(getValue(), size,(char) padCharacter.byteValue() ); } else { ISOUtil.padright(getValue(), size,(char) padCharacter.byteValue() ); } } else { throw new ISOException(String.format("Field [%s]:Cannot pack as data has size %d and size needs to be %d",getName(), value.length(),size)); } byte[] packedbyte = new byte[interpretter.getPackedLength(size)]; interpretter.interpret(getValue(), packedbyte, 0); return packedbyte; }