/** * Each supported key has a mask that represents its type. The key that has no known mask can't be processed bt the * address. * * @param k * key to calculate mask of. * * @return */ protected static int mask(AbstractKey k) { KeyInfo i = k.info(); switch (i.getAlgorythm()) { case RSAPublic: case RSAPrivate: if (((PublicKey) k).getPublicExponent() == 0x10001) { int l = i.getKeyLength(); if (l == 2048 / 8) return 0x01; if (l == 4096 / 8) return 0x02; } break; } throw new IllegalArgumentException("key can't be masked for address: " + i); }
@Test public void packKdf() throws Exception { KeyInfo h1 = new KeyInfo(KeyInfo.PRF.HMAC_SHA256, 4096, null, null); KeyInfo h2 = new KeyInfo(h1.pack()); assertEquals(h1.getAlgorythm(), h2.getAlgorythm()); assertEquals(h1.getPRF(), h2.getPRF()); assertEquals(h1.getRounds(), h2.getRounds()); assertEquals(h1.getKeyLength(), h2.getKeyLength()); assertEquals(KeyInfo.PRF.HMAC_SHA256, h2.getPRF()); assertEquals(KeyInfo.Algorythm.AES256, h2.getAlgorythm()); assertArrayEquals(h1.getTag(), h2.getTag()); }
keys.forEach(k -> { KeyInfo i = k.info(); report("\t✔︎ " + i.getAlgorythm() + ":" + i.getKeyLength() * 8 + ":" + i.getBase64Tag()); }); report("\nWhich can play roles:\n");
@Test public void symmetricKeyMustHaveRightInfo() { SymmetricKey k = new SymmetricKey(); KeyInfo h = k.info(); assertEquals(KeyInfo.Algorythm.AES256, h.getAlgorythm()); assertEquals(32, h.getKeyLength()); assertEquals(KeyInfo.PRF.None, h.getPRF()); }
@Test public void publicKeyMustHaveInfo() throws Exception { AbstractKey k = TestKeys.privateKey(3).getPublicKey(); KeyInfo h = k.info(); assertEquals(KeyInfo.Algorythm.RSAPublic, h.getAlgorythm()); assertEquals(KeyInfo.PRF.None, h.getPRF()); assertEquals(5, h.getTag().length); // Bytes.dump(h.pack()); }
@Test public void privateKeyMustHaveInfo() throws Exception { AbstractKey prk = TestKeys.privateKey(3); AbstractKey puk = prk.getPublicKey(); KeyInfo h = prk.info(); assertEquals(KeyInfo.Algorythm.RSAPrivate, h.getAlgorythm()); assertEquals(KeyInfo.PRF.None, h.getPRF()); assertEquals(5, h.getTag().length); assertArrayEquals(puk.info().getTag(), h.getTag()); // Bytes.dump(h.pack()); }
KeyInfo h1 = new KeyInfo(KeyInfo.Algorythm.RSAPublic, new byte[]{1, 2, 3}, 512); KeyInfo h2 = new KeyInfo(h1.pack()); assertEquals(h1.getAlgorythm(), h2.getAlgorythm()); assertEquals(h1.getPRF(), h2.getPRF()); assertEquals(h1.getRounds(), h2.getRounds()); assertEquals(KeyInfo.Algorythm.RSAPublic, h2.getAlgorythm()); assertEquals(h1.getAlgorythm(), h2.getAlgorythm()); assertEquals(h1.getPRF(), h2.getPRF()); assertEquals(h1.getRounds(), h2.getRounds()); assertEquals(KeyInfo.Algorythm.RSAPrivate, h2.getAlgorythm()); assertEquals(h1.getAlgorythm(), h2.getAlgorythm()); assertEquals(h1.getPRF(), h2.getPRF()); assertEquals(h1.getRounds(), h2.getRounds()); assertEquals(KeyInfo.Algorythm.AES256, h2.getAlgorythm());