private void digest(String msg, MessageDigest digest) { // Вывести используемый алгоритм System.out.println("Using algorithm: " + digest.getAlgorithm()); digest.reset(); byte[] bytes = msg.getBytes(); byte[] out = digest.digest(bytes); System.out.println(out); }
private String encode(CharSequence rawPassword, String salt) { byte[] input = Utf8.encode(salt + rawPassword); byte[] digest = messageDigest.digest(input); return messageDigest.getAlgorithm().toLowerCase() + "$" + salt + "$" + new String(Hex.encode(digest)); }
/** * Begins a new hash code computation by returning an initialized, stateful {@link * MessageDigestHasher} instance that is ready to receive data. */ MessageDigestHasher newHasher() { if (supportsClone) { try { return new SHA256HashFunction.MessageDigestHasher((MessageDigest) digest.clone(), bytes); } catch (CloneNotSupportedException e) { // falls through } } return new SHA256HashFunction.MessageDigestHasher( getMessageDigest(digest.getAlgorithm()), bytes); }
@Override public Hasher newHasher() { if (supportsClone) { try { return new MessageDigestHasher((MessageDigest) prototype.clone(), bytes); } catch (CloneNotSupportedException e) { // falls through } } return new MessageDigestHasher(getMessageDigest(prototype.getAlgorithm()), bytes); }
/** Return a MessageDigest to the pool. Must be SHA-256 ! */ public static void returnMessageDigest(MessageDigest md256) { if (md256 == null) return; String algo = md256.getAlgorithm(); if (!(algo.equals("SHA-256") || algo.equals("SHA256"))) throw new IllegalArgumentException("Should be SHA-256 but is " + algo); md256.reset(); synchronized (digests) { int mdPoolSize = digests.size(); if (mdPoolSize > MESSAGE_DIGESTS_TO_CACHE || noCache) { // don't cache too many of them if (logMINOR) Logger.normal( SHA256.class, "Throwing away a SHA256 MessageDigest (" + mdPoolSize + '>' + MESSAGE_DIGESTS_TO_CACHE + ')'); return; } digests.add(md256); } }
Object writeReplace() { return new SerializedForm(prototype.getAlgorithm(), bytes, toString); }
/** * Constructor. * * @param digest */ public Digest(final MessageDigest digest) { this.algorithm = digest.getAlgorithm(); this.content = digest.digest(); }