public void recycle(MessageDigest md) { if(this.equals(SHA256)) { freenet.crypt.SHA256.returnMessageDigest(md); } // Else no pooling. } }
/** * Gets the IV being used. Only works with algorithms that support IVs. * @return Returns the iv as a IvParameterSpec */ public IvParameterSpec getIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } return iv; } }
private void generateOutput() { counterInc(); output_buffer = new byte[counter.length]; cipher_ctx.encipher(counter, output_buffer); if(output_count++ > Pg) { output_count = 0; nextBytes(tmp); rekey(tmp); } }
public void testSerializeUnserialize() throws FSParseException { SimpleFieldSet sfs = ecdsa.asFieldSet(true); ECDSA ecdsa2 = new ECDSA(sfs.getSubset(curveToTest.name()), curveToTest); assertEquals(ecdsa.getPublicKey(), ecdsa2.getPublicKey()); }
public void testGetAgreedSecret() throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { byte[] aliceS = alice.getAgreedSecret(bob.getPublicKey()); byte[] bobS = bob.getAgreedSecret(alice.getPublicKey()); assertNotNull(aliceS); assertNotNull(bobS); assertEquals(toHex(aliceS), toHex(bobS)); assertEquals(aliceS.length, curveToTest.derivedSecretSize); assertEquals(bobS.length, curveToTest.derivedSecretSize); }
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); } }
public void testAsFieldSet() throws FSParseException { SimpleFieldSet privSFS = ecdsa.asFieldSet(true); assertNotNull(privSFS.getSubset(curveToTest.name())); assertNotNull(privSFS.get(curveToTest.name()+".pub")); assertNotNull(privSFS.get(curveToTest.name()+".pri")); // Ensure we don't leak the privkey when we don't intend to SimpleFieldSet pubSFS = ecdsa.asFieldSet(false); assertNotNull(pubSFS.getSubset(curveToTest.name())); assertNotNull(pubSFS.get(curveToTest.name()+".pub")); assertNull(pubSFS.get(curveToTest.name()+".pri")); }
/** * Converts a specified key for a specified algorithm to a PublicKey which is then stored in * a KeyPair. The private key of the KeyPair is null. Can not handle DSA keys. * @param type The type of key being passed in * @param pub Public key as byte[] * @return Public key as KeyPair with a null private key */ public static KeyPair getPublicKeyPair(KeyPairType type, byte[] pub) { return getKeyPair(getPublicKey(type, pub), null); }
private void generator_init(String cipher) { cipher_ctx = Util.getCipherByName(cipher); output_buffer = new byte[cipher_ctx.getBlockSize() / 8]; counter = new byte[cipher_ctx.getBlockSize() / 8]; allZeroString = new byte[cipher_ctx.getBlockSize() / 8]; tmp = new byte[cipher_ctx.getKeySize() / 8]; fetch_counter = output_buffer.length; }
@Override public byte[] asBytes() { byte[] groupBytes = getGroup().asBytes(); byte[] ybytes = Util.MPIbytes(y); byte[] bytes = new byte[groupBytes.length + ybytes.length]; System.arraycopy(groupBytes, 0, bytes, 0, groupBytes.length); System.arraycopy(ybytes, 0, bytes, groupBytes.length, ybytes.length); return bytes; }
/** Verify a checksum or throw */ public void verifyChecksum(byte[] data, int offset, int length, byte[] checksum) throws ChecksumFailedException { if(!checkChecksum(data, offset, length, checksum)) throw new ChecksumFailedException(); }
/** * Encrypts the provided byte[]. If you are using a RijndaelECB * alg then the length of input must equal the block size. * @param input The byte[] to be encrypted * @return The encrypted data. The original data will be unchanged. */ public byte[] encryptCopy(byte[] input){ return encryptCopy(input, 0, input.length); }
public static HashResult[] copy(HashResult[] hashes) { if(hashes == null) return null; HashResult[] out = new HashResult[hashes.length]; for(int i=0;i<hashes.length;i++) { out[i] = hashes[i].clone(); } return out; }
@Override public byte[] fingerprint() { BigInteger fp[] = new BigInteger[3]; fp[0] = p; fp[1] = q; fp[2] = g; return fingerprint(fp); }
public EncryptedRandomAccessBucket(EncryptedRandomAccessBufferType type, RandomAccessBucket underlying, MasterSecret masterKey) { this.type = type; this.underlying = underlying; this.masterKey = masterKey; baseSetup(masterKey); }
/** * Initialize the ECDH Exchange: this will draw some entropy * @param curve */ public ECDH(Curves curve) { this.curve = curve; this.key = curve.generateKeyPair(); }
@Override public byte[] fingerprint() { synchronized(this) { if(fingerprint == null) fingerprint = fingerprint(new BigInteger[]{y}); return fingerprint; } }
@Override public int read() throws IOException { int length = read(onebyte); if(length <= 0) return -1; else return onebyte[0]; }