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:baldrickv.s3streamingtool.Hash.java

public static String hash(String algo, int output_bits, byte b[], int offset, int size) {
    try {//from   w w w  . java2 s  .  c o m
        int output_bytes = output_bits / 4; //hex = 4 bits per byte
        MessageDigest sig = MessageDigest.getInstance(algo);
        sig.update(b, offset, size);
        byte d[] = sig.digest();

        StringBuffer s = new StringBuffer(output_bytes);
        BigInteger bi = new BigInteger(1, d);
        s.append(bi.toString(16));
        while (s.length() < output_bytes) {
            s.insert(0, '0');
        }
        return s.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
        return null;
    }

}

From source file:Main.java

/**
 * Convert an array of bytes to a non-negative big integer.
 *//*from   w  ww .  j  a  v a2s . c om*/
public static BigInteger ByteArrayToBigIntegerWithoutSign(byte[] array) {
    return new BigInteger(1, array);
}

From source file:de.csdev.ebus.command.datatypes.std.AbstractEBusTypeUnsignedNumber.java

@Override
public BigDecimal decodeInt(byte[] data) throws EBusTypeException {

    byte[] clone = ArrayUtils.clone(data);
    ArrayUtils.reverse(clone);//from  ww  w  . ja v a 2s  . c o m

    return new BigDecimal(new BigInteger(1, clone));
}

From source file:com.algodefu.yeti.md5.MD5HashGenerator.java

public static String generateKeyByString(String string) {
    MessageDigest m = null;/*from  w w  w.ja  v  a  2s  .  c  om*/
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.reset();
    m.update(string.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:de.upb.wdqa.wdvd.revisiontags.SHA1Converter.java

private static byte[] parseByte(int base, String sha1) {
    byte[] result = null;
    if (sha1 != null) {
        if (!sha1.equals("")) {
            try {
                BigInteger bi = new BigInteger(sha1, base);
                result = bi.toByteArray();

            } catch (Exception e) {
                logger.error("", e);
            }//from   www .j  a  v a2  s .c  om
        } else {
            result = new byte[0];
        }
    }
    return result;

}

From source file:org.rivetlogic.utils.IntegrationUtils.java

public static BigInteger MD5BigInteger(String str) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("MD5");

    byte[] dg = digest.digest(str.getBytes());

    BigInteger number = new BigInteger(1, dg);

    return number;
}

From source file:Pusher.java

/**
 * Converts a byte array to a string representation
 * @param data// ww  w  .  ja  v a  2 s .c  o  m
 * @return
 */
private static String byteArrayToString(byte[] data) {
    BigInteger bigInteger = new BigInteger(1, data);
    String hash = bigInteger.toString(16);
    // Zero pad it
    while (hash.length() < 32) {
        hash = "0" + hash;
    }
    return hash;
}

From source file:com.sugaronrest.restapicalls.methodcalls.Authentication.java

/**
 * Login to SugarCRM via REST API call./*from  w w  w .j a  va 2 s.c o m*/
 *
 *  @param loginRequest LoginRequest object.
 *  @return LoginResponse object.
 */
public static LoginResponse login(LoginRequest loginRequest) throws Exception {

    LoginResponse loginResponse = new LoginResponse();

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        String passwordHash = new BigInteger(1, md5.digest(loginRequest.password.getBytes())).toString(16);

        Map<String, String> userCredentials = new LinkedHashMap<String, String>();
        userCredentials.put("user_name", loginRequest.username);
        userCredentials.put("password", passwordHash);

        Map<String, Object> auth = new LinkedHashMap<String, Object>();
        auth.put("user_auth", userCredentials);
        auth.put("application_name", "RestClient");

        ObjectMapper mapper = new ObjectMapper();
        String jsonAuthData = mapper.writeValueAsString(auth);

        Map<String, Object> request = new LinkedHashMap<String, Object>();
        request.put("method", "login");
        request.put("input_type", "json");
        request.put("response_type", "json");
        request.put("rest_data", auth);

        String jsonRequest = mapper.writeValueAsString(request);

        request.put("rest_data", jsonAuthData);

        HttpResponse response = Unirest.post(loginRequest.url).fields(request).asString();

        String jsonResponse = response.getBody().toString();
        loginResponse.setJsonRawRequest(jsonRequest);
        loginResponse.setJsonRawResponse(jsonResponse);

        Map<String, Object> responseObject = mapper.readValue(jsonResponse, Map.class);
        if (responseObject.containsKey("id")) {
            loginResponse.sessionId = (responseObject.get("id").toString());
            loginResponse.setStatusCode(response.getStatus());
            loginResponse.setError(null);
        } else {
            ErrorResponse errorResponse = mapper.readValue(jsonResponse, ErrorResponse.class);
            errorResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            loginResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            loginResponse.setError(errorResponse);
        }
    } catch (Exception exception) {
        ErrorResponse errorResponse = ErrorResponse.format(exception, exception.getMessage());
        loginResponse.setError(errorResponse);
    }

    return loginResponse;
}

From source file:Main.java

public static String getMd5Hash(File file) {
    try {// w ww.  java2 s .co m
        // CTS (6/15/2010) : stream file through digest instead of handing it the byte[]
        MessageDigest md = MessageDigest.getInstance("MD5");
        int chunkSize = 256;

        byte[] chunk = new byte[chunkSize];

        // Get the size of the file
        long lLength = file.length();

        if (lLength > Integer.MAX_VALUE) {
            Log.e(t, "File " + file.getName() + "is too large");
            return null;
        }

        int length = (int) lLength;

        InputStream is = null;
        is = new FileInputStream(file);

        int l = 0;
        for (l = 0; l + chunkSize < length; l += chunkSize) {
            is.read(chunk, 0, chunkSize);
            md.update(chunk, 0, chunkSize);
        }

        int remaining = length - l;
        if (remaining > 0) {
            is.read(chunk, 0, remaining);
            md.update(chunk, 0, remaining);
        }
        byte[] messageDigest = md.digest();

        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);
        while (md5.length() < 32)
            md5 = "0" + md5;
        is.close();
        return md5;

    } catch (NoSuchAlgorithmException e) {
        Log.e("MD5", e.getMessage());
        return null;

    } catch (FileNotFoundException e) {
        Log.e("No Cache File", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Problem reading from file", e.getMessage());
        return null;
    }

}

From source file:com.assemblade.server.model.AccessToken.java

public static AccessToken createAccessToken(User user) {
    AccessToken token = new AccessToken();
    token.uid = user.getUserId();/*  w w  w  .  j ava  2  s . c  om*/
    token.token = Base64.encode(SequenceNumberGenerator.getNextSequenceNumber().getBytes());
    token.secret = new BigInteger(130, secureRandom).toString();
    return token;
}