Example usage for org.bouncycastle.util.encoders HexEncoder HexEncoder

List of usage examples for org.bouncycastle.util.encoders HexEncoder HexEncoder

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders HexEncoder HexEncoder.

Prototype

public HexEncoder() 

Source Link

Usage

From source file:com.nokia.xfolite.xforms.xpath.XFormsCoreFunctionLibrary.java

License:Open Source License

public static String digest_function(String s, String method, String encoding) {
    //#debug info
    System.out.println("Digest function invoked with params(" + s + "," + method + "," + encoding + ")");
    Digest digest = null;/*  w  w w .j  a v  a 2  s . c  o  m*/
    Encoder encoder = null;
    if (encoding.equals("base64")) {
        encoder = new Base64Encoder();
    } else if (encoding.equals("hex")) {
        encoder = new HexEncoder();
    }
    if (method.equals("SHA-1")) {
        digest = new SHA1Digest();
    } else if (method.equals("SHA-256")) {
        digest = new SHA256Digest();
    } else if (method.equals("SHA-512")) {
        digest = new SHA512Digest();
    } else if (method.equals("MD5")) {
        digest = new MD5Digest();
    }

    if (encoder == null) {
        throw new XPathException(XPathException.TYPE_ERR,
                "XForms function digest() only supports hex and base64 encoding.");
    }
    if (digest == null) {
        throw new XPathException(XPathException.TYPE_ERR,
                "XForms function digest() only supports MD5, SHA-1, SHA-256 and SHA-512 digests.");
    }

    int len = s.length();
    for (int i = 0; i < len; i++) {
        digest.update((byte) s.charAt(i)); // FIXME: Better not use non-ASCII characters!
    }

    byte[] data = new byte[digest.getDigestSize()];
    digest.doFinal(data, 0);

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        encoder.encode(data, 0, data.length, bOut);
    } catch (IOException e) {
        throw new XPathException(XPathException.TYPE_ERR, "Exception when encoding digest: " + e);
    }

    byte[] out = bOut.toByteArray();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < out.length; i++) {
        sb.append((char) out[i]); // This works fine, neither hex nor base64 encodings produce 
    }
    return sb.toString();
}

From source file:net.ripe.rpki.commons.crypto.util.KeyPairUtil.java

License:BSD License

static String hexEncodeHashData(byte[] keyHashData) {
    HexEncoder hexEncoder = new HexEncoder();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//  w  ww  . j a v a 2  s  .c o m
        hexEncoder.encode(keyHashData, 0, keyHashData.length, out);
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException("Exception hex encoding data", e);
    }
}

From source file:org.xwiki.crypto.internal.encoder.HexBinaryStringEncoder.java

License:Open Source License

@Override
InternalBinaryStringEncoder getEncoder() {
    return new AbstractBouncyCastleInternalBinaryStringEncoder(new HexEncoder(), BLOCK_SIZE, CHAR_SIZE) {
        @Override//from   w w w  .  ja  v a2 s.c o m
        public boolean isValidEncoding(byte b) {
            return ((b >= 0x2f && b <= 0x39) || (b >= 0x41 && b <= 0x46) || (b >= 0x61 && b <= 0x66));
        }
    };
}