/** * Given a piece of text and a message signature encoded in base64, returns an ECKey * containing the public key that was used to sign it. This can then be compared to the expected public key to * determine if the signature was correct. * * @param messageHash a piece of human readable text that was signed * @param signatureBase64 The Ethereum-format message signature in base64 * * @return - * @throws SignatureException If the public key could not be recovered or if there was a signature format error. */ public static byte[] signatureToKeyBytes(byte[] messageHash, String signatureBase64) throws SignatureException { byte[] signatureEncoded; try { signatureEncoded = Base64.decode(signatureBase64); } catch (RuntimeException e) { // This is what you get back from Bouncy Castle if base64 doesn't decode :( throw new SignatureException("Could not decode base64", e); } // Parse the signature bytes into r/s and the selector value. if (signatureEncoded.length < 65) throw new SignatureException("Signature truncated, expected 65 bytes and got " + signatureEncoded.length); return signatureToKeyBytes( messageHash, ECDSASignature.fromComponents( Arrays.copyOfRange(signatureEncoded, 1, 33), Arrays.copyOfRange(signatureEncoded, 33, 65), (byte) (signatureEncoded[0] & 0xFF))); }
/** * * @return - */ public String toBase64() { byte[] sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes for S sigData[0] = v; System.arraycopy(bigIntegerToBytes(this.r, 32), 0, sigData, 1, 32); System.arraycopy(bigIntegerToBytes(this.s, 32), 0, sigData, 33, 32); return new String(Base64.encode(sigData), Charset.forName("UTF-8")); }
/** * Delete metadata entry */ public void deleteMetadata(String payload) throws IOException, InvalidCipherTextException, MetadataException { byte[] encryptedPayloadBytes; if (isEncrypted) { //base64 to buffer encryptedPayloadBytes = Base64.decode(AESUtil.encryptWithKey(encryptionKey, payload)); } else { encryptedPayloadBytes = payload.getBytes("utf-8"); } byte[] message = MetadataUtil.message(encryptedPayloadBytes, magicHash); String signature = node.signMessage(new String(Base64.encode(message))); Call<Void> response = getApiInstance().deleteMetadata(address, signature); Response<Void> exe = response.execute(); if (!exe.isSuccessful()) { throw new MetadataException(exe.code() + " " + exe.message()); } else { magicHash = null; } }
public static String toBase64String( byte[] data, int off, int length) { byte[] encoded = encode(data, off, length); return Strings.fromByteArray(encoded); }
@Override public String write(byte[] value) throws Exception { return Base64.toBase64String(value); }
encryptedPayloadBytes = Base64.decode(AESUtil.encryptWithKey(encryptionKey, payload)); } else { encryptedPayloadBytes = payload.getBytes("utf-8"); String signature = node.signMessage(new String(Base64.encode(message))); body.setPayload(new String(Base64.encode(encryptedPayloadBytes))); body.setSignature(signature); body.setPrevMagicHash(magicHash != null ? Hex.toHexString(magicHash) : null);
private void saveIvToFile(final byte[] iv) { final String toWrite = Base64.toBase64String(iv); //this.preferences.edit().putString(IV, toWrite).apply(); }
/** * <p>base64Decode.</p> * * @param string a {@link java.lang.String} object. * @return an array of byte. */ public static byte[] base64Decode(String string) { return Base64.decode(string); }
/** * encode the input data producing a base 64 encoded byte array. * * @return a byte array containing the base 64 encoded data. */ public static byte[] encode( byte[] data) { return encode(data, 0, data.length); }
public static String Base64ToString(byte[] data) { return Base64.toBase64String(data); }
public static byte[] Base64ToBytes(String data) { return Base64.decode(data); }
/** {@inheritDoc} */ @Override public String base64Encode(byte[] bytes) { try { return new String(Base64.encode(bytes), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
@Override public String write(UUID value) throws Exception { if(value == null) { return Base64.toBase64String(new byte[0]); } return Base64.toBase64String(ByteUtils.uuidToBytes(value)); }
@Override public byte[] read(String value) throws Exception { return Base64.decode(value.getBytes()); }
/** {@inheritDoc} */ @Override public String base64Encode(byte[] bytes) { try { return new String(Base64.encode(bytes), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
private String annotateRequest(byte[] data) { int i = 0; StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); // pw.print("-----BEGIN CERTIFICATE REQUEST-----\n"); do { if (i + 48 < data.length) { pw.print(Base64.toBase64String(data, i, 48)); i += 48; } else { pw.print(Base64.toBase64String(data, i, data.length - i)); i = data.length; } pw.print('\n'); } while (i < data.length); // pw.print("-----END CERTIFICATE REQUEST-----\n"); pw.flush(); return sw.toString(); }
/** {@inheritDoc} */ @Override public byte[] base64Decode(String string) { return Base64.decode(StringUtils.removeWhitespace(string)); }
/** * <p>base64Encode.</p> * * @param bytes an array of byte. * @return a {@link java.lang.String} object. */ public static String base64Encode(byte[] bytes) { try { return new String(Base64.encode(bytes), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * <p>Get an SSH key from the compressed EC public key in base64</p> * * @param publicKey The ecdsa-sha2-nistp256 EC public key * * @return An ssh key-only base64 format of public key from given EC public key */ public static String decompressSSHKeyFromNistp256(ECPublicKey publicKey) { ByteBuffer buffer = ByteBuffer.allocate(104); buffer.putInt(KEY_PREFIX.getBytes(Charsets.UTF_8).length); buffer.put(KEY_PREFIX.getBytes(Charsets.UTF_8)); buffer.putInt(CURVE_NAME.getBytes(Charsets.UTF_8).length); buffer.put(CURVE_NAME.getBytes(Charsets.UTF_8)); byte[] octet = {(byte) 0x04}; // this is special byte for SSH byte[] x = publicKey.getW().getAffineX().toByteArray(); // get X, Y cords of ECPoint byte[] y = publicKey.getW().getAffineY().toByteArray(); byte[] x32 = ByteUtils.subArray(x, x.length - 32, x.length); //get last 32 bytes byte[] y32 = ByteUtils.subArray(y, y.length - 32, y.length); // Ignore the y32 warning here in Intellij - it's just a naming mismatch in parameters byte[] data = ByteUtils.concatenate(octet, ByteUtils.concatenate(x32, y32)); buffer.putInt(data.length); buffer.put(data); return Base64.toBase64String(buffer.array()); }
/** {@inheritDoc} */ @Override public byte[] base64Decode(String string) { return Base64.decode(StringUtils.removeWhitespace(string)); }