public static void writeXML(SolrInputDocument doc, Writer writer) throws IOException { writer.write("<doc boost=\"" + doc.getDocumentBoost() + "\">"); for (SolrInputField field : doc) { float boost = field.getBoost(); String name = field.getName(); for (Object v : field) { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format((Date) v); } else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0, bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64( bytes.array(), bytes.position(), bytes.limit() - bytes.position()); } if (boost != 1.0f) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name); } // only write the boost for the first multi-valued field // otherwise, the used boost is the product of all the boost values boost = 1.0f; } } writer.write("</doc>"); }
public static void main(String[] args) throws Exception { RSAKeyPair keyPair = new RSAKeyPair(); System.out.println(keyPair.getPublicKeyStr()); PublicKey pk = deserializeX509PublicKey(keyPair.getPublicKeyStr()); byte[] payload = "Hello World!".getBytes(StandardCharsets.UTF_8); byte[] encrypted = keyPair.encrypt(ByteBuffer.wrap(payload)); String cipherBase64 = Base64.byteArrayToBase64(encrypted); System.out.println("encrypted: " + cipherBase64); System.out.println("signed: " + Base64.byteArrayToBase64(keyPair.signSha256(payload))); System.out.println( "decrypted " + new String(decryptRSA(encrypted, pk), StandardCharsets.UTF_8)); }
/** Marshals a binary field value. */ protected static Object marshalBase64SortValue(Object value) { if (null == value) { return null; } final BytesRef val = (BytesRef) value; return Base64.byteArrayToBase64(val.bytes, val.offset, val.length); }
/** Unmarshals a binary field value. */ protected static Object unmarshalBase64SortValue(Object value) { if (null == value) { return null; } final String val = (String) value; final byte[] bytes = Base64.base64ToByteArray(val); return new BytesRef(bytes); }
public static PublicKey deserializeX509PublicKey(String pubKey) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.base64ToByteArray(pubKey)); return keyFactory.generatePublic(publicKeySpec); } catch (Exception e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } }
public static String decodeAES(String base64CipherTxt, String pwd, final int keySizeBits) { final Charset ASCII = Charset.forName("ASCII"); final int INDEX_KEY = 0; final int INDEX_IV = 1; final int ITERATIONS = 1; final int SALT_OFFSET = 8; final int SALT_SIZE = 8; final int CIPHERTEXT_OFFSET = SALT_OFFSET + SALT_SIZE; try { byte[] headerSaltAndCipherText = Base64.base64ToByteArray(base64CipherTxt); // --- extract salt & encrypted --- // header is "Salted__", ASCII encoded, if salt is being used (the default) byte[] salt = Arrays.copyOfRange(headerSaltAndCipherText, SALT_OFFSET, SALT_OFFSET + SALT_SIZE); byte[] encrypted = Arrays.copyOfRange( headerSaltAndCipherText, CIPHERTEXT_OFFSET, headerSaltAndCipherText.length); // --- specify cipher and digest for evpBytesTokey method --- Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding"); MessageDigest md5 = MessageDigest.getInstance("MD5"); // --- create key and IV --- // the IV is useless, OpenSSL might as well have use zero's final byte[][] keyAndIV = evpBytesTokey( keySizeBits / Byte.SIZE, aesCBC.getBlockSize(), md5, salt, pwd.getBytes(ASCII), ITERATIONS); SecretKeySpec key = new SecretKeySpec(keyAndIV[INDEX_KEY], "AES"); IvParameterSpec iv = new IvParameterSpec(keyAndIV[INDEX_IV]); // --- initialize cipher instance and decrypt --- aesCBC.init(Cipher.DECRYPT_MODE, key, iv); byte[] decrypted = aesCBC.doFinal(encrypted); return new String(decrypted, ASCII); } catch (BadPaddingException e) { // AKA "something went wrong" throw new IllegalStateException( "Bad password, algorithm, mode or padding;" + " no salt, wrong number of iterations or corrupted ciphertext.", e); } catch (IllegalBlockSizeException e) { throw new IllegalStateException("Bad algorithm, mode or corrupted (resized) ciphertext.", e); } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } }
private static SimpleOrderedMap<Object> getDocumentFieldsInfo( Document doc, int docId, IndexReader reader, IndexSchema schema) throws IOException { SimpleOrderedMap<Object> finfo = new SimpleOrderedMap<Object>(); for (Object o : doc.getFields()) { Fieldable fieldable = (Fieldable) o; SimpleOrderedMap<Object> f = new SimpleOrderedMap<Object>(); SchemaField sfield = schema.getFieldOrNull(fieldable.name()); FieldType ftype = (sfield == null) ? null : sfield.getType(); f.add("type", (ftype == null) ? null : ftype.getTypeName()); f.add("schema", getFieldFlags(sfield)); f.add("flags", getFieldFlags(fieldable)); Term t = new Term( fieldable.name(), ftype != null ? ftype.storedToIndexed(fieldable) : fieldable.stringValue()); f.add("value", (ftype == null) ? null : ftype.toExternal(fieldable)); // TODO: this really should be "stored" f.add("internal", fieldable.stringValue()); // may be a binary number byte[] arr = fieldable.getBinaryValue(); if (arr != null) { f.add("binary", Base64.byteArrayToBase64(arr, 0, arr.length)); } f.add("boost", fieldable.getBoost()); f.add( "docFreq", t.text() == null ? 0 : reader.docFreq(t)); // this can be 0 for non-indexed fields // If we have a term vector, return that if (fieldable.isTermVectorStored()) { try { TermFreqVector v = reader.getTermFreqVector(docId, fieldable.name()); if (v != null) { SimpleOrderedMap<Integer> tfv = new SimpleOrderedMap<Integer>(); for (int i = 0; i < v.size(); i++) { tfv.add(v.getTerms()[i], v.getTermFrequencies()[i]); } f.add("termVector", tfv); } } catch (Exception ex) { log.warn("error writing term vector", ex); } } finfo.add(fieldable.name(), f); } return finfo; }
/** Try with all signatures and return the name of the signature that matched */ public String verify(String sig, ByteBuffer data) { exception = null; for (Map.Entry<String, PublicKey> entry : keys.entrySet()) { boolean verified; try { verified = CryptoKeys.verify(entry.getValue(), Base64.base64ToByteArray(sig), data); log.info("verified {} ", verified); if (verified) return entry.getKey(); } catch (Exception e) { exception = e; log.info("NOT verified "); } } return null; }
public RSAKeyPair() { KeyPairGenerator keyGen = null; try { keyGen = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } keyGen.initialize(512); java.security.KeyPair keyPair = keyGen.genKeyPair(); privateKey = keyPair.getPrivate(); publicKey = keyPair.getPublic(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); pubKeyStr = Base64.byteArrayToBase64(x509EncodedKeySpec.getEncoded()); }