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:com.networknt.utility.HashUtil.java

public static String md5(String input) {

    String md5 = null;/*w  w  w.ja  va  2 s .  c om*/

    if (null == input)
        return null;

    try {

        //Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");

        //Update input string in message digest
        digest.update(input.getBytes(UTF_8), 0, input.length());

        //Converts message digest value in base 16 (hex)
        md5 = new BigInteger(1, digest.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    return md5;
}

From source file:com.hobba.hobaserver.services.security.TokenUtil.java

public String getToken(String kid, String expiration_time) {
    HobaKeysFacadeREST hkfrest = new HobaKeysFacadeREST();
    HobaKeys hk = hkfrest.findHKIDbyKID(kid);
    HobaDevices hd = hk.getIdDevices();/*w w w  .j av a 2  s  .c om*/
    HobaUser hu = hd.getIduser();

    SecureRandom random = new SecureRandom();
    String rand = new BigInteger(256, random).toString(32);
    HobaToken ht = new HobaToken();
    ht.setToken(rand);
    long time = 0;
    try {
        time = Long.parseLong(expiration_time);
        if (time > 0) {
            Date date = new Date(new Date().getTime() + (time * 1000));
            ht.setExpiration(date);
        } else {
            ht.setExpiration(null);
        }
    } catch (Exception e) {
        ht.setExpiration(null);
    }
    ht.setIsValid(Boolean.TRUE);
    ht.setIdUser(hu);

    HobaTokenFacadeREST htfrest = new HobaTokenFacadeREST();
    ht = htfrest.create(ht);
    String token = kid + ":" + rand;
    byte[] encodedBytes = Base64.encodeBase64(token.getBytes());
    token = new String(encodedBytes);
    return token;
}

From source file:org.shareok.data.redis.RedisUtil.java

/**
 *This works by choosing 130 bits from a cryptographically secure random bit generator, and encoding them in base-32. 128 bits is considered to be cryptographically strong, 
 * but each digit in a base 32 number can encode 5 bits, so 128 is rounded up to the next multiple of 5. 
 * This encoding is compact and efficient, with 5 random bits per character. Compare this to a random UUID, 
 * which only has 3.4 bits per character in standard layout, and only 122 random bits in total.
 * /*from ww w.  j  a  v  a  2  s. co  m*/
 * If you allow session identifiers to be easily guessable (too short, flawed random number generator, etc.), 
 * attackers can hijack other's sessions. Note that SecureRandom objects are expensive to initialize, so you'll want to keep one around and reuse it.
 * 
 * @return a secure, random string
 */
public static String getRandomString() {
    SecureRandom random = new SecureRandom();
    return new BigInteger(130, random).toString(32);
}

From source file:com.github.aelstad.keccakj.core.TestPermutation.java

String hexWordsToHexBytes(String hexWords) {
    StringBuilder rv = new StringBuilder();
    byte[] buf = new byte[8];
    for (String word : hexWords.split(" ")) {
        if (word.trim().isEmpty())
            continue;
        long l = new BigInteger(word, 16).longValue();
        for (int i = 0; i < 8; ++i) {
            buf[i] = (byte) ((l >>> (8 * i)) & 0xFF);
        }/*w  w  w  . j  a  va2  s.co  m*/
        rv.append(org.apache.commons.codec.binary.Hex.encodeHex(buf));
    }
    return rv.toString();
}

From source file:tn.insat.controllers.InscriptionController.java

public void handleFileUpload(FileUploadEvent event) {
    InputStream inputStream = null;
    BigInteger random = new BigInteger(64, new Random());
    String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/accountPics/");
    System.out.println(path);// www.  j av a 2 s. c  om
    try {
        inputStream = event.getFile().getInputstream();
        FileOutputStream outputStream = new FileOutputStream(
                path + "\\" + random + event.getFile().getFileName());
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while (true) {
            bytesRead = inputStream.read(buffer);
            if (bytesRead > 0) {
                outputStream.write(buffer, 0, bytesRead);
            } else {
                break;
            }
        }
        outputStream.close();
        inputStream.close();
        realImagePath = path + "\\" + random + event.getFile().getFileName();
        relativeImagePath = random + event.getFile().getFileName();
        sendMessage(Utilities.getBundleMessage("phr", "success.download"), FacesMessage.SEVERITY_INFO);

    } catch (IOException ex) {
        sendMessage(Utilities.getBundleMessage("err", "error.download"), FacesMessage.SEVERITY_ERROR);
        System.out.println(ex.getMessage());
    }
}

From source file:Main.java

public static byte[] genSirfCommand(String commandHexa) {
    int length = commandHexa.length() / 2;
    ByteBuffer command = ByteBuffer.allocate(length);
    command.put(new BigInteger(commandHexa, 16).toByteArray(), 1, length);
    return command.array();
}

From source file:eu.cloudwave.wp5.feedbackhandler.model.factories.ApplicationFactoryImpl.java

/**
 * {@inheritDoc}/* ww w . ja  v  a 2  s.  com*/
 */
@Override
public DbApplication create(final String applicationId) {
    final String accessToken = new BigInteger(130, new SecureRandom()).toString(32);
    return new DbApplicationImpl(applicationId, accessToken);
}

From source file:Main.java

/**
 *Valid plugin  md5//from  w  ww . ja v  a2 s .co m
 * @param path   bundle archvie path
 * @param md5Sum   target  file md5
 * @return  if md5 matched,return true
 * ***/
public static boolean validFileMD5(String path, String md5Sum) {

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        File mFile = new File(path);
        if (mFile == null || !mFile.exists() || !mFile.isFile()) {
            return false;
        }
        FileInputStream in = new FileInputStream(mFile);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, mFile.length());
        messageDigest.update(byteBuffer);
        String digest = String.format("%032x", new BigInteger(1, messageDigest.digest()));
        return md5Sum.equals(digest.toString());
    } catch (NoSuchAlgorithmException e) {

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

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

    }
    return false;
}

From source file:Main.java

/**
 * Computes the base 16 representation of the byte array argument.
 *
 * @param bytes an array of bytes.//from  ww w.j av a  2 s .c om
 * @return the bytes represented as a string of hexadecimal digits.
 */
public static String toHexString(byte[] bytes) {
    BigInteger bi = new BigInteger(1, bytes);
    return String.format("%0" + (bytes.length << 1) + "X", bi);
}

From source file:com.vangent.hieos.services.xds.bridge.utils.UUIDUtils.java

/**
 * Method description/*from www.ja v  a  2 s .  c o m*/
 *
 *
 * @param uuid
 * @param prefix
 *
 * @return
 */
public static String toOID(UUID uuid, String prefix) {

    String result = null;

    if ((uuid != null) && StringUtils.isNotBlank(prefix)) {

        String uuidstr = uuid.toString().replaceAll("-", "");
        BigInteger bi = new BigInteger(uuidstr, 16);

        result = String.format("%s.%s", prefix, bi.toString());
    }

    return result;
}