PlaintextKeysProvider implements CardKeysProvider { @Override public Optional<GPCardKeys> getCardKeys(String spec) { if (spec == null) return Optional.empty(); if ("default".startsWith(spec.toLowerCase())) return Optional.of(PlaintextKeys.defaultKey()); try { for (PlaintextKeys.Diversification d : PlaintextKeys.Diversification.values()) { if (spec.toLowerCase().startsWith(d.name().toLowerCase())) { byte[] k = HexUtils.stringToBin(spec.substring(d.name().length() + 1)); return Optional.of(PlaintextKeys.derivedFromMasterKey(k, null, d)); } } byte[] k = HexUtils.stringToBin(spec); return Optional.of(PlaintextKeys.fromMasterKey(k)); } catch (IllegalArgumentException e) { } return Optional.empty(); } PlaintextKeysProvider(); @Override Optional<GPCardKeys> getCardKeys(String spec);  }
@Test public void testGarbage() { CardKeysProvider p = new PlaintextKeysProvider(); Assert.assertFalse(p.getCardKeys("404142434445464748494a4b4c4d4e4fXX").isPresent()); }
@Test public void testMasterKey() { CardKeysProvider p = new PlaintextKeysProvider(); Assert.assertTrue(p.getCardKeys("404142434445464748494a4b4c4d4e4f").isPresent()); }
@Test public void testDiversification() { CardKeysProvider p = new PlaintextKeysProvider(); Optional<GPCardKeys> pk = p.getCardKeys("emv:404142434445464748494a4b4c4d4e4f"); Assert.assertTrue(pk.isPresent()); }
@Test public void testUnknownDiversification() { CardKeysProvider p = new PlaintextKeysProvider(); Optional<GPCardKeys> pk = p.getCardKeys("foobar:404142434445464748494a4b4c4d4e4f"); Assert.assertFalse(pk.isPresent()); }