Example usage for org.bouncycastle.util Arrays copyOfRange

List of usage examples for org.bouncycastle.util Arrays copyOfRange

Introduction

In this page you can find the example usage for org.bouncycastle.util Arrays copyOfRange.

Prototype

public static BigInteger[] copyOfRange(BigInteger[] original, int from, int to) 

Source Link

Usage

From source file:tor.util.ByteFifo.java

License:Open Source License

public synchronized byte[] get(int bytes) {
    byte buf[] = new byte[buffer.length];
    int cnt = 0;//w ww . j  ava 2s  .  co  m
    while (count > 0 && (bytes == -1 || cnt < bytes)) {
        buf[cnt++] = buffer[out];
        count--;
        out = (out + 1) % buffer.length;
    }
    return Arrays.copyOfRange(buf, 0, cnt);
}

From source file:us.eharning.atomun.keygen.internal.spi.bip0032.BouncyCastleBIP0032NodeProcessor.java

License:Apache License

/**
 * Convert the Base58+checksum encoded string into a BIP0032 node.
 *
 * @param serialized/*from  www  . j  a  v a  2s  . c  o  m*/
 *         encoded string to decode.
 *
 * @return BIP0032 node represented by the serialized string.
 *
 * @throws ValidationException
 *         if the data is not a valid encoded BIP0032 node.
 */
@Override
public BIP0032Node importNode(String serialized) throws ValidationException {
    byte[] data = Base58.decodeWithChecksum(serialized);
    if (data.length != 78) {
        throw new ValidationException("Invalid extended key value");
    }
    byte[] type = Arrays.copyOf(data, 4);
    boolean hasPrivate;
    if (Arrays.areEqual(type, xprv) || Arrays.areEqual(type, tprv)) {
        hasPrivate = true;
    } else if (Arrays.areEqual(type, xpub) || Arrays.areEqual(type, tpub)) {
        hasPrivate = false;
    } else {
        throw new ValidationException("Invalid magic number for an extended key");
    }
    int depth = data[4] & 0xff;
    int parent = getInt32(data, 5);
    int sequence = getInt32(data, 9);
    byte[] chainCode = Arrays.copyOfRange(data, 13, 13 + 32);
    byte[] pubOrPriv = Arrays.copyOfRange(data, 13 + 32, data.length);
    ECKey key;
    if (hasPrivate) {
        key = ECKeyFactory.getInstance().fromSecretExponent(new BigInteger(1, pubOrPriv), true);
    } else {
        key = ECKeyFactory.getInstance().fromEncodedPublicKey(pubOrPriv, true);
    }
    return new BIP0032Node(key, chainCode, depth, parent, sequence);
}

From source file:us.eharning.atomun.keygen.internal.spi.bip0032.BouncyCastleBIP0032NodeProcessor.java

License:Apache License

/**
 * Generates a BIP0032 node from a seed value that is passed through a basic HMAC process.
 *
 * @param seed/* w w  w  .j a v  a  2s.  com*/
 *         value for which the node should be generated.
 *
 * @return BIP0032 node deterministically based on the seed input.
 *
 * @throws ValidationException
 *         if either cryptography fails (unlikely)
 *         or the seed results in an invalid EC key (unlikely).
 */
/* BITCOIN_SEED is not a password but instead a shared key to mask the seed input */
@SuppressFBWarnings("HARD_CODE_PASSWORD")
@SuppressWarnings("checkstyle:localvariablename")
@Override
public BIP0032Node generateNodeFromSeed(byte[] seed) throws ValidationException {
    try {
        Mac mac = Mac.getInstance("HmacSHA512");
        SecretKey seedkey = new SecretKeySpec(BITCOIN_SEED, "HmacSHA512");
        mac.init(seedkey);
        byte[] lr = mac.doFinal(seed);
        byte[] l = Arrays.copyOfRange(lr, 0, 32);
        byte[] r = Arrays.copyOfRange(lr, 32, 64);
        BigInteger m = new BigInteger(1, l);
        if (m.compareTo(curve.getN()) >= 0 || m.compareTo(BigInteger.ZERO) == 0) {
            throw new ValidationException("Invalid chain value generated");
        }
        ECKey keyPair = ECKeyFactory.getInstance().fromSecretExponent(m, true);
        return new BIP0032Node(keyPair, r, 0, 0, 0);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new ValidationException(e);
    }
}

From source file:Utils.bytes.Bytes.java

public Bytes copyOfRange(int from, int to) {
    return new Bytes(Arrays.copyOfRange(bytes, from, to));
}

From source file:Utils.bytes.Bytes.java

public void splice(int from, int to) {
    byte[] first = Arrays.copyOf(bytes, from);
    byte[] second = Arrays.copyOfRange(bytes, to, this.bytes.length);

    this.bytes = Arrays.concatenate(first, second);
}