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

static public String digest(String input) {
    try {/*from w  w  w . j a v  a 2  s .c  o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        return new BigInteger(1, md.digest(input.getBytes())).toString(16).toUpperCase();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

static String getGeneratedUdid() {
    SecureRandom localSecureRandom = new SecureRandom();
    return new BigInteger(64, localSecureRandom).toString(16);
}

From source file:Main.java

/**
 * Transforms a byte array to a//from   ww  w  .j a  v  a  2s  .  c  o m
 * positive number string
 * @param in byte array
 * @return positive number string
 */
public static String bytesToPositiveNumString(byte[] in) {
    BigInteger num = new BigInteger(1, in);
    return num.toString();
}

From source file:Main.java

public static String getMd5(String source) {
    try {//from   ww  w  .  j  a va2 s.  co  m
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(source.getBytes());
        BigInteger bi = new BigInteger(1, md5.digest());
        return String.format("%032x", bi);
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String convertToMD5(String toEnc) {
    try {//  w w  w .j  a  va  2  s . co  m
        MessageDigest mdEnc = MessageDigest.getInstance("MD5");
        mdEnc.update(toEnc.getBytes(), 0, toEnc.length());
        return new BigInteger(1, mdEnc.digest()).toString(16);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Key getRSAKey(String hexModulus, String hexPrivateExponent) throws Exception {
    BigInteger m = new BigInteger(hexModulus, 16);
    BigInteger e = new BigInteger(hexPrivateExponent, 16);
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key rsaKey = keyFactory.generatePrivate(keySpec);
    return rsaKey;
}

From source file:Main.java

public static String getStringMD5(String str) {
    String value = null;//from  ww  w.  j  a  v a2 s. c  o m
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(str.getBytes());
        BigInteger bi = new BigInteger(1, md5.digest());
        value = bi.toString(16).toUpperCase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static String hashMD5(String s) {
    try {/* ww w. j  a va2 s. c  o m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        String result = new BigInteger(1, m.digest()).toString();
        while (result.length() < 32) {
            result = "0" + result;
        }
        return result;
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

private static String escapeSql(String value) {
    if (value == null) {
        return "NULL";
    }/* w ww  .ja v a 2s.  c  o  m*/
    String hexValue;
    try {
        hexValue = String.format("%x", new BigInteger(1, value.getBytes("UTF-8"))); // NOTE: implicitly assuming here database is using UTF-8 encoding
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Unsupported character");
    }
    return "X'" + hexValue + "'";
}

From source file:Main.java

public static String SHA256Encrypt(String strForEncrypt) {
    byte[] data = getHash(strForEncrypt);
    return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data)).toLowerCase();
}