Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

In this page you can find the example usage for java.security MessageDigest digest.

Prototype

public byte[] digest(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:fr.ippon.wip.ltpa.token.LtpaLibrary.java

private static byte[] createHash(byte[] buffer) throws NoSuchAlgorithmException {
    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    return sha1.digest(buffer);
}

From source file:com.yilang.commons.utils.util.Digests.java

private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {/* ww  w .  j  a  v  a  2 s .  c om*/
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

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

/**
 * Login to SugarCRM via REST API call./*from w w  w . jav  a  2s .com*/
 *
 *  @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:eds.component.encryption.EncryptionUtility.java

public static String getHash(String transactionId, EncryptionType type) {
    String secureHash = transactionId;
    MessageDigest md;
    String hashedID = "";
    byte[] hash;/*from  w  w w. j a  v a2s .  c o m*/
    try {
        //md = MessageDigest.getInstance("SHA-256");
        md = MessageDigest.getInstance(type.toString());
        hash = md.digest(secureHash.getBytes("UTF-8"));
        hashedID = new String(Hex.encodeHex(hash));//String.format("%032x", new BigInteger(hash));;//String.format("%032x", Arrays.toString(hash));
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        throw new RuntimeException("No such algorithm or encoding method: " + ex.getMessage());
    }

    return hashedID;
}

From source file:ch.newscron.encryption.Encryption.java

/**
 * Given a JSONObject, extracts its fields and computes the hash using the MD5 algorithm
 * @param obj is a JSONObject consisting of the four fields
 * @return a MD5 hash of type byte[]/* ww  w  .j av a2  s . c om*/
 */
public static byte[] createMD5Hash(JSONObject obj) {
    //Create a string of the fields with format: "<customerId>$<rew1>$<rew2>$<val>"
    StringJoiner stringToHash = new StringJoiner("$");
    stringToHash.add((String) obj.get("custID"));
    stringToHash.add((String) obj.get("rew1"));
    stringToHash.add((String) obj.get("rew2"));
    stringToHash.add((String) obj.get("val"));

    //Get hash of string  
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] hash = m.digest(stringToHash.toString().getBytes("UTF-8"));
        return hash;
    } catch (Exception e) {
    }

    return null;
}

From source file:DigestUtil.java

static String getDigestedValue(String type, byte[] data, String charset) {
    try {/*from  w  w  w.ja  v a 2s  .co m*/
        MessageDigest digest = MessageDigest.getInstance(type);
        return getHexString(digest.digest(data), charset);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:DigestUtil.java

static String getDigestedValue(String type, byte[] data) {
    try {/*from   ww w.  jav  a  2  s . com*/
        MessageDigest digest = MessageDigest.getInstance(type);
        return getHexString(digest.digest(data));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.alliander.osgp.oslp.OslpUtils.java

private static byte[] createHash(final byte[] message) throws GeneralSecurityException {
    // Create digest Hash
    final MessageDigest digest = MessageDigest.getInstance(FALLBACK_DIGEST);
    return digest.digest(message);
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;/*  w ww.j  a  v a2s.c o m*/
    try {
        cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        Ticket ticket = new Ticket(username, hash, ticketValidity);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(ticket);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        byte[] digest = md.digest(bos.toByteArray());
        bos.write(digest);
        byte[] encryptedBytes = cipher.doFinal(bos.toByteArray());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException
            | BadPaddingException | IllegalBlockSizeException e) {
        LOGGER.log(Level.SEVERE, "Error when making a ticket", e);
    }
    return "";
}

From source file:function.Functions.java

protected static String MD5(String text) {
    String md5Text = "";
    try {//from w  w w. j a v  a2s.  c o m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        md5Text = new BigInteger(1, digest.digest((text).getBytes())).toString(16);
    } catch (Exception e) {
        System.out.println("Error in call to MD5");
    }

    if (md5Text.length() == 31) {
        md5Text = "0" + md5Text;
    }
    return md5Text;
}