Example usage for java.math BigInteger BigInteger

List of usage examples for java.math BigInteger BigInteger

Introduction

In this page you can find the example usage for java.math BigInteger BigInteger.

Prototype

private BigInteger(byte[] magnitude, int signum) 

Source Link

Document

This private constructor is for internal use and assumes that its arguments are correct.

Usage

From source file:Main.java

public static RSAPrivateKey privKeyFromJwk(String jwkp) {
    RSAPrivateKey privKey = null;

    try {//from ww w  . j  ava  2  s. c  om
        JSONObject jk = new JSONObject(jwkp).getJSONArray("keys").getJSONObject(0);

        BigInteger n = new BigInteger(1, decodeB64(jk.getString("n")));
        BigInteger d = new BigInteger(1, decodeB64(jk.getString("d")));

        RSAPrivateKeySpec privRsaSpec = new RSAPrivateKeySpec(n, d);
        KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC");
        privKey = (RSAPrivateKey) keyfact.generatePrivate(privRsaSpec);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return privKey;
}

From source file:Main.java

public static String getJwkPublic(KeyPair kp) {
    try {/*from  w ww . j  a  va 2  s  .  c  om*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid 
        SecureRandom random = new SecureRandom();
        String kid = new BigInteger(130, random).toString(32);
        jk.put("kid", kid);
        jk.put("e", "AQAB");

        KeyFactory kfactory = KeyFactory.getInstance("RSA");

        RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);

        jk.put("n", encodeB64(kspec.getModulus().toByteArray()));
        JSONArray ja = new JSONArray();
        ja.put(jk);
        JSONObject jo = new JSONObject();
        jo.put("keys", ja);

        // Log.d("getJwkPublic key: ",pubkey.toString());
        // Log.d("getJwkPublic jwk: ",jo.toString());

        return jo.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String getJwkPrivate(KeyPair kp) {
    try {/*from  w w w .j av a  2s . c o  m*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid 
        SecureRandom random = new SecureRandom();
        String kid = new BigInteger(130, random).toString(32);
        jk.put("kid", kid);
        jk.put("e", "AQAB");

        KeyFactory kfactory = KeyFactory.getInstance("RSA");

        RSAPrivateKeySpec privkspec = (RSAPrivateKeySpec) kfactory.getKeySpec(kp.getPrivate(),
                RSAPrivateKeySpec.class);
        RSAPublicKeySpec pubkspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
                RSAPublicKeySpec.class);

        // Log.d("getJwkPrivate n",pubkspec.getPublicExponent().toString());
        // Log.d("getJwkPrivate d",privkspec.getPrivateExponent().toString());

        jk.put("n", encodeB64(pubkspec.getModulus().toByteArray()));
        jk.put("d", encodeB64(privkspec.getPrivateExponent().toByteArray()));
        JSONArray ja = new JSONArray();
        ja.put(jk);
        JSONObject jo = new JSONObject();
        jo.put("keys", ja);

        return jo.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.alibaba.otter.shared.common.utils.ByteUtils.java

public static BigInteger hexToBigInt(String hex) {
    return new BigInteger(hex, 16);
}

From source file:HashUtil.java

public static String getMD5(final String data) {
    try {/*from   w w  w .  j av  a 2s . c om*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(data.getBytes());
        BigInteger bigInt = new BigInteger(1, m.digest());
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:Hex.java

/**
 * Decodar hex till binrt/*from  w w  w . j  av a  2s  . c o  m*/
 *
 *@param dataStr Strng innehllande hex-representation av data
 *@return byte[] innhllande binr representation av data
 **/
public static byte[] decode(String dataStr) {

    if ((dataStr.length() & 0x01) == 0x01)
        dataStr = new String(dataStr + "0");
    BigInteger cI = new BigInteger(dataStr, 16);
    byte[] data = cI.toByteArray();

    return data;
}

From source file:Main.java

/**
 * Converts an array of bytes to a string of hexadecimal characters.
 * Leading null bytes are preserved in the output.
 * <p>//from w  w  w  . j a va2 s. co m
 * The input byte stream is assumed to be a positive, two's complement
 * representation of an integer.  The return value is the hexadecimal string
 * representation of this value.
 * 
 * @param bytes  the bytes to convert
 * @return       the string representation
 */
public static String bytesToHexString(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
        return "";
    }
    BigInteger bigint = new BigInteger(1, bytes);
    int formatLen = bytes.length * 2;
    return String.format("%0" + formatLen + "x", bigint);
}

From source file:cn.edu.bit.whitesail.utils.MD5Signature.java

public static String calculate(byte[] byteArray) {
    StringBuffer result = null;/* w  w w  .ja va  2s  . c om*/
    if (byteArray == null)
        return null;
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(byteArray);
        result = new StringBuffer(new BigInteger(1, m.digest()).toString(16));
        for (int i = 0; i < 32 - result.length(); i++)
            result.insert(0, '0');
    } catch (NoSuchAlgorithmException ex) {
        LOG.fatal("MD5 Hashing Failed,System is going down");
        System.exit(1);
    }
    return result.toString();
}

From source file:Main.java

public static String getMD5(String value) {
    if (value == null || value.length() == 0)
        return null;

    MessageDigest m = null;//  ww w .  j a  va  2 s  . com

    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    m.update(value.getBytes(), 0, value.length());
    return new BigInteger(1, m.digest()).toString(16).toUpperCase(Locale.getDefault());
}

From source file:Main.java

/**
 *  http://download.oracle.com/javase/1.5.0/docs/guide/security/CryptoSpec.html
 *  Signature Format   ASN.1 sequence of two INTEGER values: r and s, in that order:
 *                                SEQUENCE ::= { r INTEGER, s INTEGER }
 *
 *  http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One
 *  30 -- tag indicating SEQUENCE//from www.  j  a v  a2s.  c om
 *  xx - length in octets
 *
 *  02 -- tag indicating INTEGER
 *  xx - length in octets
 *  xxxxxx - value
 *
 *  Convert to BigInteger and back so we have the minimum length representation, as required.
 *  r and s are always non-negative.
 *
 *  Only supports sigs up to about 252 bytes. See code to fix BER encoding for this before you
 *  add a SigType with bigger signatures.
 *
 *  @throws IllegalArgumentException if too big
 *  @since 0.8.7, moved to SigUtil in 0.9.9
 */
private static byte[] sigBytesToASN1(byte[] sig) {
    //System.out.println("pre TO asn1\n" + net.i2p.util.HexDump.dump(sig));
    int len = sig.length;
    int sublen = len / 2;
    byte[] tmp = new byte[sublen];

    System.arraycopy(sig, 0, tmp, 0, sublen);
    BigInteger r = new BigInteger(1, tmp);
    byte[] rb = r.toByteArray();
    if (rb.length > 127)
        throw new IllegalArgumentException("FIXME R length > 127");
    System.arraycopy(sig, sublen, tmp, 0, sublen);
    BigInteger s = new BigInteger(1, tmp);
    byte[] sb = s.toByteArray();
    if (sb.length > 127)
        throw new IllegalArgumentException("FIXME S length > 127");
    int seqlen = rb.length + sb.length + 4;
    if (seqlen > 255)
        throw new IllegalArgumentException("FIXME seq length > 255");
    int totlen = seqlen + 2;
    if (seqlen > 127)
        totlen++;
    byte[] rv = new byte[totlen];
    int idx = 0;

    rv[idx++] = 0x30;
    if (seqlen > 127)
        rv[idx++] = (byte) 0x81;
    rv[idx++] = (byte) seqlen;

    rv[idx++] = 0x02;
    rv[idx++] = (byte) rb.length;
    System.arraycopy(rb, 0, rv, idx, rb.length);
    idx += rb.length;

    rv[idx++] = 0x02;
    rv[idx++] = (byte) sb.length;
    System.arraycopy(sb, 0, rv, idx, sb.length);

    //System.out.println("post TO asn1\n" + net.i2p.util.HexDump.dump(rv));
    return rv;
}