private String generateAccessTokenHash(final AccessToken accessTokenId, final OidcRegisteredService service) { final byte[] tokenBytes = accessTokenId.getId().getBytes(); final String hashAlg; switch (signingService.getJsonWebKeySigningAlgorithm()) { case AlgorithmIdentifiers.RSA_USING_SHA512: hashAlg = MessageDigestAlgorithms.SHA_512; break; case AlgorithmIdentifiers.RSA_USING_SHA256: default: hashAlg = MessageDigestAlgorithms.SHA_256; } LOGGER.debug("Digesting access token hash via algorithm [{}]", hashAlg); final byte[] digested = DigestUtils.rawDigest(hashAlg, tokenBytes); final byte[] hashBytesLeftHalf = Arrays.copyOf(digested, digested.length / 2); return EncodingUtils.encodeBase64(hashBytesLeftHalf); }
public static void main(String[] args) throws Exception { MessageDigest md = MessageDigest.getInstance(MessageDigestAlgorithms.MD5); md.update("texto a cifrar".getBytes()); byte[] digest = md.digest(); // Se escribe byte a byte en hexadecimal for (byte b : digest) { System.out.print(Integer.toHexString(0xFF & b)); } System.out.println(); // Se escribe codificado base 64. Se necesita la librer�a // commons-codec-x.x.x.jar de Apache byte[] encoded = Base64.encodeBase64(digest); System.out.println(new String(encoded)); }
private void run() throws IOException { if (algorithm.equalsIgnoreCase("ALL") || algorithm.equals("*")) { run(MessageDigestAlgorithms.values()); return; } final MessageDigest messageDigest = DigestUtils.getDigest(algorithm, null); if (messageDigest != null) { run("", messageDigest); } else { run("", DigestUtils.getDigest(algorithm.toUpperCase(Locale.ROOT))); } }
/** * The hashing function used by the Cache. * * @param key * @return * @throws VictimsException */ protected String hash(String key) throws VictimsException { try { MessageDigest mda = MessageDigest .getInstance(MessageDigestAlgorithms.SHA_256); return Hex.encodeHexString(mda.digest(key.getBytes())); } catch (NoSuchAlgorithmException e) { throw new VictimsException(String.format("Could not hash key: %s", key), e); } }
/** * Hash the file to get a "unique" key for caching * * @param file * file to hash * @param name * canonical file name * @return name + md5 hash of file * @throws VictimsException */ private static String hashFile(File file, String name) throws VictimsException { InputStream fis = null; try { fis = new FileInputStream(file); byte[] buffer = new byte[1024]; MessageDigest mda = MessageDigest .getInstance(MessageDigestAlgorithms.MD5); int numRead; do { numRead = fis.read(buffer); if (numRead > 0) { mda.update(buffer, 0, numRead); } } while (numRead != -1); return name + Hex.encodeHexString(mda.digest()); } catch (NoSuchAlgorithmException e) { throw new VictimsException(String.format("Could not hash file: %s", name), e); } catch (IOException io) { throw new VictimsException(String.format("Could not open file: %s", name), io); } finally { IOUtils.closeQuietly(fis); } }
private static byte[] sha1(String s) throws NoSuchAlgorithmException { MessageDigest SHA1 = MessageDigest.getInstance(MessageDigestAlgorithms.SHA_1); SHA1.reset(); SHA1.update(s.getBytes()); return SHA1.digest(); }
/** * Computes hex encoded SHA512 digest. * * @param data data to be hashed * @return sha-512 hash */ public static String sha512(final String data) { return digest(MessageDigestAlgorithms.SHA_512, data.getBytes(StandardCharsets.UTF_8)); }
/** * Computes hex encoded SHA256 digest. * * @param data data to be hashed * @return sha-256 hash */ public static String sha256(final String data) { return digest(MessageDigestAlgorithms.SHA_256, data.getBytes(StandardCharsets.UTF_8)); }
/** * Computes hex encoded SHA digest. * * @param data data to be hashed * @return sha hash */ public static String sha(final String data) { return digest(MessageDigestAlgorithms.SHA_1, data); }
/** * Computes SHA digest. * * @param data data to be hashed * @return sha hash */ public static byte[] sha(final byte[] data) { return rawDigest(MessageDigestAlgorithms.SHA_1, data); }