Example usage for org.apache.commons.codec.binary Base32 Base32

List of usage examples for org.apache.commons.codec.binary Base32 Base32

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base32 Base32.

Prototype

public Base32() 

Source Link

Usage

From source file:tor.HiddenService.java

public static void sendIntroduce(TorSocket sock, String onion, TorCircuit rendz) throws IOException {
    log.debug("Fetching Hidden Service Descriptor");
    String hsdescTxt = fetchHSDescriptor(sock, onion);
    OnionRouter rendzOR = rendz.getLastHop().router;

    // parse the hidden service descriptor
    TorDocumentParser hsdesc = new TorDocumentParser(hsdescTxt);
    //decode the intro points
    String intopointsb64 = new String(Base64.decode(hsdesc.map.get("introduction-points")));
    // parse intro points document
    TorDocumentParser intros = new TorDocumentParser(intopointsb64);
    // get first intro point
    String introPointIdentities[] = intros.getArrayItem("introduction-point");

    int introPointNum = 0;

    String ip0 = Hex.encodeHexString(new Base32().decode(introPointIdentities[introPointNum].toUpperCase()));
    OnionRouter ip0or = Consensus.getConsensus().routers.get(ip0);
    byte[] serviceKey = Base64.decode(intros.getArrayItem("service-key")[introPointNum]);
    byte skHash[] = TorCrypto.getSHA1().digest(serviceKey);
    assert (skHash.length == 20);
    log.debug("Using Intro Point: {}, building circuit...", ip0or);

    TorCircuit ipcirc = sock.createCircuit(true);
    ipcirc.create();/*from  w w w.j a v  a2s .c om*/
    ipcirc.extend(ip0or);

    // outer packet
    ByteBuffer buf = ByteBuffer.allocate(1024);
    buf.put(skHash); // service PKhash

    // inner handshake
    ByteBuffer handshake = ByteBuffer.allocate(1024);
    handshake.put((byte) 2); //ver
    handshake.put(rendzOR.ip.getAddress()); // rendz IP addr
    handshake.putShort((short) rendzOR.orport);
    try {
        handshake.put(new Hex().decode(rendzOR.identityhash.getBytes()));
    } catch (DecoderException e) {
        e.printStackTrace();
    }
    handshake.putShort((short) rendzOR.onionKeyRaw.length); // rendz key len
    handshake.put(rendzOR.onionKeyRaw); // rendz key
    handshake.put(rendz.rendezvousCookie); //rend cookie

    // tap handshake / create handshake
    byte priv_x[] = new byte[128];
    TorCrypto.rnd.nextBytes(priv_x); // g^x
    rendz.temp_x = TorCrypto.byteToBN(priv_x);
    rendz.temp_r = null;

    BigInteger pubKey = TorCrypto.DH_G.modPow(rendz.temp_x, TorCrypto.DH_P);
    byte pubKeyByte[] = TorCrypto.BNtoByte(pubKey);
    handshake.put(pubKeyByte);

    handshake.flip();

    // convert to byte array
    byte handshakeBytes[] = new byte[handshake.remaining()];
    handshake.get(handshakeBytes);

    // encrypt handshake
    PublicKey skPK = TorCrypto.asn1GetPublicKey(serviceKey);
    buf.put(TorCrypto.hybridEncrypt(handshakeBytes, skPK));

    buf.flip();
    byte introcell[] = new byte[buf.remaining()];
    buf.get(introcell);

    ipcirc.send(introcell, TorCircuit.RELAY_COMMAND_INTRODUCE1, false, (short) 0);
    log.debug("waiting for introduce acknowledgement");
    ipcirc.waitForState(TorCircuit.STATES.INTRODUCED, false);

    log.debug("Now waiting for rendezvous connect");
    rendz.waitForState(TorCircuit.STATES.RENDEZVOUS_COMPLETE, false);

    ipcirc.destroy(); // no longer needed
    log.debug("Hidden Service circuit built");
}

From source file:vellum.crypto.topt.Totps.java

public static String generateSecret() {
    byte[] buffer = new byte[10];
    new SecureRandom().nextBytes(buffer);
    return new String(new Base32().encode(buffer));
}

From source file:vellum.crypto.topt.Totps.java

public static boolean verifyCode(String secret, int code, long timeIndex, int variance)
        throws NoSuchAlgorithmException, InvalidKeyException {
    Base32 codec = new Base32();
    byte[] decodedKey = codec.decode(secret);
    for (int i = -variance; i <= variance; i++) {
        if (getCode(decodedKey, timeIndex + i) == code) {
            return true;
        }//  w w w  .jav a 2s. c  om
    }
    return false;
}

From source file:vellum.crypto.topt.Totps.java

static List<Integer> getCodeList(String secret, long timeIndex, int variance)
        throws NoSuchAlgorithmException, InvalidKeyException {
    List<Integer> list = new ArrayList();
    for (int i = -variance; i <= variance; i++) {
        list.add(getCode(new Base32().decode(secret), timeIndex + i));
    }//from  w ww.j a v  a  2s  .c o  m
    return list;
}

From source file:vellum.crypto.topt.Totps.java

public static int getCode(String secret, long timeIndex) throws NoSuchAlgorithmException, InvalidKeyException {
    return getCode(new Base32().decode(secret), timeIndex);
}