public static byte[] digest(byte[] data) { MessageDigest md = null; try { md = getMessageDigest(); return md.digest(data); } finally { returnMessageDigest(md); } }
public void recycle(MessageDigest md) { if(this.equals(SHA256)) { freenet.crypt.SHA256.returnMessageDigest(md); } // Else no pooling. } }
public static byte[] getCryptoKey(byte[] hash) { // This is exactly the same algorithm used by e.g. JFK for generating multiple session keys from a single generated value. // The only difference is we use a constant of more than one byte's length here, to avoid having to keep a registry. MessageDigest md = SHA256.getMessageDigest(); md.update(hash); md.update(SPLITKEY); byte[] buf = md.digest(); SHA256.returnMessageDigest(md); return buf; }
private static byte[] makeRoutingKey(byte[] pkHash, byte[] ehDocname) { MessageDigest md256 = SHA256.getMessageDigest(); md256.update(ehDocname); md256.update(pkHash); byte[] key = md256.digest(); SHA256.returnMessageDigest(md256); return key; }
public static byte[] getCrossSegmentSeed(byte[] hash) { // This is exactly the same algorithm used by e.g. JFK for generating multiple session keys from a single generated value. // The only difference is we use a constant of more than one byte's length here, to avoid having to keep a registry. MessageDigest md = SHA256.getMessageDigest(); md.update(hash); md.update(CROSS_SEGMENT_SEED); byte[] buf = md.digest(); SHA256.returnMessageDigest(md); return buf; }
public byte[] saltKey(Key key) { MessageDigest md = SHA256.getMessageDigest(); md.update(key.getRoutingKey()); md.update(globalSalt); byte[] ret = md.digest(); SHA256.returnMessageDigest(md); return ret; }
private int getInitialMessageID(byte[] identity) { MessageDigest md = SHA256.getMessageDigest(); md.update(identity); // Similar to JFK keygen, should be safe enough. try { md.update("INITIAL0".getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new Error(e); } byte[] hashed = md.digest(); SHA256.returnMessageDigest(md); return Fields.bytesToInt(hashed, 0); }
private int getInitialMessageID(byte[] identity, byte[] otherIdentity) { MessageDigest md = SHA256.getMessageDigest(); md.update(identity); md.update(otherIdentity); // Similar to JFK keygen, should be safe enough. try { md.update("INITIAL1".getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new Error(e); } byte[] hashed = md.digest(); SHA256.returnMessageDigest(md); return Fields.bytesToInt(hashed, 0); }
private byte[] localSaltKey(Key key) { MessageDigest md = SHA256.getMessageDigest(); md.update(key.getRoutingKey()); md.update(localSalt); byte[] ret = md.digest(); SHA256.returnMessageDigest(md); return ret; }
/** * Get digested routing key * * @param plainKey * @return */ byte[] getDigestedKey(byte[] plainKey) { ByteArrayWrapper key = new ByteArrayWrapper(plainKey); synchronized (digestRoutingKeyCache) { byte[] dk = digestRoutingKeyCache.get(key); if (dk != null) return dk; } MessageDigest digest = SHA256.getMessageDigest(); try { digest.update(plainKey); digest.update(salt); byte[] hashedRoutingKey = digest.digest(); assert hashedRoutingKey.length == 0x20; synchronized (digestRoutingKeyCache) { digestRoutingKeyCache.put(key, hashedRoutingKey); } return hashedRoutingKey; } finally { SHA256.returnMessageDigest(digest); } }
/** * Convert the key to a double between 0.0 and 1.0. * Normally we will hash the key first, in order to * make chosen-key attacks harder. */ public synchronized double toNormalizedDouble() { if(cachedNormalizedDouble > 0) return cachedNormalizedDouble; MessageDigest md = SHA256.getMessageDigest(); if(routingKey == null) throw new NullPointerException(); md.update(routingKey); int TYPE = getType(); md.update((byte)(TYPE >> 8)); md.update((byte)TYPE); byte[] digest = md.digest(); SHA256.returnMessageDigest(md); md = null; long asLong = Math.abs(Fields.bytesToLong(digest)); // Math.abs can actually return negative... if(asLong == Long.MIN_VALUE) asLong = Long.MAX_VALUE; cachedNormalizedDouble = ((double)asLong)/((double)Long.MAX_VALUE); return cachedNormalizedDouble; }
public static byte[] hash(Bucket data) throws IOException { InputStream is = data.getInputStreamUnbuffered(); try { MessageDigest md = SHA256.getMessageDigest(); try { long bucketLength = data.size(); long bytesRead = 0; byte[] buf = new byte[BUFFER_SIZE]; while ((bytesRead < bucketLength) || (bucketLength == -1)) { int readBytes = is.read(buf); if (readBytes < 0) break; bytesRead += readBytes; if (readBytes > 0) md.update(buf, 0, readBytes); } if ((bytesRead < bucketLength) && (bucketLength > 0)) throw new EOFException(); if ((bytesRead != bucketLength) && (bucketLength > 0)) throw new IOException("Read " + bytesRead + " but bucket length " + bucketLength + " on " + data + '!'); byte[] retval = md.digest(); return retval; } finally { SHA256.returnMessageDigest(md); } } finally { if(is != null) is.close(); } }
SHA256.hash(fis, md); byte[] hash = md.digest(); SHA256.returnMessageDigest(md); fis.close(); fis = null;
SHA256.returnMessageDigest(hash); } catch(Exception e) { throw new PluginNotFoundException("Error while computing hash '"+digest+"' of the downloaded plugin: " + e, e);
public static ClientKSK create(String keyword) { MessageDigest md256 = SHA256.getMessageDigest(); try { byte[] keywordHash; try { keywordHash = md256.digest(keyword.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new Error("Impossible: JVM doesn't support UTF-8: " + e, e); } MersenneTwister mt = new MersenneTwister(keywordHash); DSAPrivateKey privKey = new DSAPrivateKey(Global.DSAgroupBigA, mt); DSAPublicKey pubKey = new DSAPublicKey(Global.DSAgroupBigA, privKey); byte[] pubKeyHash = md256.digest(pubKey.asBytes()); try { return new ClientKSK(keyword, pubKeyHash, pubKey, privKey, keywordHash); } catch (MalformedURLException e) { throw new Error(e); } } finally { SHA256.returnMessageDigest(md256); } }
byte[] finalHash = md256.digest(data); SHA256.returnMessageDigest(md256);
SHA256.returnMessageDigest(md256); md256 = null;
md.update(data); byte[] hash = md.digest(); SHA256.returnMessageDigest(md); if(key == null) { chk = new NodeCHK(hash, cryptoAlgorithm);
public static boolean verifyWithSHA256(byte[] K, byte[] text, byte[] mac) { MessageDigest sha256 = null; try { sha256 = SHA256.getMessageDigest(); HMAC_legacy hash = new HMAC_legacy(sha256); return hash.verify(K, text, mac); } finally { if(sha256 != null) SHA256.returnMessageDigest(sha256); } } }
public static byte[] macWithSHA256(byte[] K, byte[] text, int macbytes) { MessageDigest sha256 = null; try { sha256 = SHA256.getMessageDigest(); HMAC_legacy hash = new HMAC_legacy(sha256); return hash.mac(K, text, macbytes); } finally { if(sha256 != null) SHA256.returnMessageDigest(sha256); } }