@Override public PemPriv getPrivateKey(String privId) throws MException { PemBlock key = keys.get(privId); if (key == null) return null; return new PemKey( key ); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("-----BEGIN ").append(getName()).append("-----\n"); for (java.util.Map.Entry<String, Object> item : entrySet()) sb.append(item.getKey()).append(": ").append(item.getValue()).append('\n'); sb.append('\n'); if (secret) { // sb.append( Block.encodeSecret(getEncodedBlock()) ); sb.append( "?" ); } else sb.append(getEncodedBlock()); sb.append("\n\n"); sb.append("-----END ").append(getName()).append("-----\n"); return sb.toString(); } }
public static PemPriv cipherPrivFromString(String str) throws ParseException, NotSupportedException, IOException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPriv key = MVaultUtil.adaptTo(entry, PemPriv.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':').toUpperCase().trim(); String key = MString.afterIndex(str, ':').trim(); return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, name).setBlock(key); }
@Override public String getMethod() throws MException { return getString(PemBlock.METHOD); }
public static PemPriv signPrivFromString(String str) throws Exception, NotSupportedException, IOException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPriv key = MVaultUtil.adaptTo(entry, PemPriv.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':'); String key = MString.afterIndex(str, ':'); return new PemKey(PemBlock.BLOCK_SIGN).set(PemBlock.METHOD, name).setBlock(key); }
public static PemPub signPubFromString(String str) throws NotSupportedException, IOException, ParseException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPub key = MVaultUtil.adaptTo(entry, PemPub.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':'); String key = MString.afterIndex(str, ':'); return new PemKey(PemBlock.BLOCK_SIGN).set(PemBlock.METHOD, name).setBlock(key); }
@Override public PemPub getPublicKey(String pubId) { PemBlock key = keys.get(pubId); if (key == null) return null; return new PemKey( key ); }
public static PemPub cipherPubFromString(String str) throws ParseException, NotSupportedException, IOException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPub key = MVaultUtil.adaptTo(entry, PemPub.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':'); String key = MString.afterIndex(str, ':'); return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, name).setBlock(key); }
public static PemKey toKey(String key) throws ParseException { return new PemKey(new PemBlockModel().parse(key)); }
@Override public PemPair createKeys(IProperties properties) throws MException { int length = properties.getInt(CryptApi.LENGTH, 256); length = length / 8 * 8; byte[] key = new byte[length/8]; MRandom random = MApi.lookup(MRandom.class); for (int i = 0; i < key.length; i++) key[i] = random.getByte(); UUID privId = UUID.randomUUID(); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, key, true ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, length) .set(PemBlock.IDENT, privId); return new PemKeyPair(xpriv, xpriv); }
privBytes = Blowfish.encrypt(privBytes, passphrase); PemKey xpub = new PemKey(PemBlock.BLOCK_PUB , pub.getEncoded(), false ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, pub.getFormat()) .set(PemBlock.IDENT, pubId) .set(PemBlock.PRIV_ID, privId); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, privBytes, true ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, priv.getFormat()) .set(PemBlock.PUB_ID, pubId); if (MString.isSet(passphrase)) xpriv.set(PemBlock.ENCRYPTED, PemBlock.ENC_BLOWFISH);
privBytes = Blowfish.encrypt(privBytes, passphrase); PemKey xpub = new PemKey(PemBlock.BLOCK_PUB , pub.getEncoded(), false ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, pub.getFormat()) .set(PemBlock.IDENT, pubId) .set(PemBlock.PRIV_ID, privId); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, privBytes, true ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, priv.getFormat()) xpriv.set(PemBlock.ENCRYPTED, PemBlock.ENC_BLOWFISH); privBytes = null; return new PemKeyPair(xpriv, xpub);
privBytes = Blowfish.encrypt(privBytes, passphrase); PemKey xpub = new PemKey(PemBlock.BLOCK_PUB , pub.getEncoded(), false ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, pub.getFormat()) .set(PemBlock.IDENT, pubId) .set(PemBlock.PRIV_ID, privId); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, privBytes, true ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, priv.getFormat()) xpriv.set(PemBlock.ENCRYPTED, PemBlock.ENC_BLOWFISH); privBytes = null; return new PemKeyPair(xpriv, xpub);
privBytes = Blowfish.encrypt(privBytes, passphrase); PemKey xpub = new PemKey(PemBlock.BLOCK_PUB , pub.getEncoded(), false ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, pub.getFormat()) .set(PemBlock.IDENT, pubId) .set(PemBlock.PRIV_ID, privId); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, privBytes, true ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, priv.getFormat()) .set(PemBlock.PUB_ID, pubId); if (MString.isSet(passphrase)) xpriv.set(PemBlock.ENCRYPTED, PemBlock.ENC_BLOWFISH);
privBytes = Blowfish.encrypt(privBytes, passphrase); PemKey xpub = new PemKey(PemBlock.BLOCK_PUB , pub.getEncoded(), false ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, pub.getFormat()) .set(PemBlock.PRIV_ID, privId); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, privBytes, true ) .set(PemBlock.METHOD, getName()) .set(PemBlock.LENGTH, len) .set(PemBlock.FORMAT, priv.getFormat()) xpriv.set(PemBlock.ENCRYPTED, PemBlock.ENC_BLOWFISH); privBytes = null; return new PemKeyPair(xpriv, xpub);
privBytes = Blowfish.encrypt(privBytes, passphrase); PemKey xpub = new PemKey(PemBlock.BLOCK_PUB , pub.getEncoded(), false ) .set(PemBlock.METHOD, getName()) .set("StdName", stdName) .set(PemBlock.FORMAT, pub.getFormat()) .set(PemBlock.IDENT, pubId) .set(PemBlock.PRIV_ID, privId); PemKey xpriv = new PemKey(PemBlock.BLOCK_PRIV, privBytes, true ) .set(PemBlock.METHOD, getName()) .set("StdName", stdName) .set(PemBlock.FORMAT, priv.getFormat()) xpriv.set(PemBlock.ENCRYPTED, PemBlock.ENC_BLOWFISH); privBytes = null; return new PemKeyPair(xpriv, xpub);