List of usage examples for org.bouncycastle.crypto.digests SHA512Digest SHA512Digest
public SHA512Digest()
From source file:bluecrystal.service.service.SignVerifyService.java
License:Open Source License
private Digest getHashById(int hashId) { Digest ret = null;//w w w . j ava 2 s . c o m switch (hashId) { case DerEncoder.NDX_SHA1: ret = new SHA1Digest(); break; case DerEncoder.NDX_SHA224: ret = new SHA224Digest(); break; case DerEncoder.NDX_SHA256: ret = new SHA256Digest(); break; case DerEncoder.NDX_SHA384: ret = new SHA384Digest(); break; case DerEncoder.NDX_SHA512: ret = new SHA512Digest(); break; default: break; } return ret; }
From source file:co.runrightfast.core.security.bc.SHA512DigestCalculator.java
License:Apache License
@Override public byte[] getDigest() { final byte[] bytes = bos.toByteArray(); bos.reset();/*from www . j a va2 s . co m*/ final Digest sha512 = new SHA512Digest(); sha512.update(bytes, 0, bytes.length); byte[] digest = new byte[sha512.getDigestSize()]; sha512.doFinal(digest, 0); return digest; }
From source file:cologne.eck.dr.op.crypto.password_hashing.Battcrypt_v0.java
License:Open Source License
@Override public byte[] hashPassword(int outlen, byte[] in, byte[] salt, int t_cost, int m_cost, Object... varArgs) throws DataLengthException, IllegalStateException, InvalidCipherTextException { SHA512Digest sha = new SHA512Digest(); int[] data = new int[DATA_SIZE_INT]; BlowfishEngine blowfish;/* w w w . j a v a2s . c o m*/ long upgradeLoops = 1; long loops; int memSize = 4 << m_cost;//= 4 * 2 ** m_cost int memMask = memSize - 1; int[] mem; byte[] hashBuffer = new byte[HASH_LENGTH_BYTE];// holds hash value as bytes byte[] dataBuffer = new byte[DATA_SIZE_BYTE];// holds encrypted bytes // These are the PHP max. values if (m_cost > 18 || // maximum: 2.147.483.648 bytes (t_cost & 0xffff) > 62 || (t_cost >> 16) > 63 || outlen > HASH_LENGTH_BYTE) { throw new IllegalArgumentException("invalid parameters"); } int tmp = t_cost >> 16; if (tmp != 0) { // upgradeLoops = 1, 2, 3, 4, 6, 8, 12, 16, ... upgradeLoops = (long) (3 - (tmp & 1)) << ((tmp - 1) >> 1); } // loops = 2, 3, 4, 6, 8, 12, 16, ... tmp = t_cost & 0xffff; loops = (long) ((tmp & 1) + 2) << (tmp >> 1); // key = SHA512(SHA512(salt) || in) byte[] keyBytes = new byte[HASH_LENGTH_BYTE]; sha.update(salt, 0, salt.length); sha.doFinal(keyBytes, 0); sha.reset(); sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.update(in, 0, in.length);//password sha.doFinal(keyBytes, 0); sha.reset(); if (wipePassword == true) { Arrays.fill(in, (byte) 0); } // initialize cipher with 448 bit (56 byte) key: // truncate keyBytes: byte[] blowfishKey = new byte[56]; System.arraycopy(keyBytes, 0, blowfishKey, 0, 56); // use zeros as IV byte[] iv = new byte[IV_LENGTH_BYTE]; KeyParameter params = new KeyParameter(blowfishKey); Arrays.fill(blowfishKey, (byte) 0); ParametersWithIV ivParams = new ParametersWithIV(params, iv); blowfish = new BlowfishEngine(); // CBC, no padding: all vectors are multiples of Blowfish block length BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(blowfish)); cipher.init(true, ivParams); // initialize memory-hard vector: mem = new int[DATA_SIZE_INT * memSize]; for (long u = 0; u < upgradeLoops; u++) { // initialize data: // data = SHA512(BIG_ENDIAN_64( 0) || key) || ... // ... || SHA512(BIG_ENDIAN_64(31) || key) byte[] counterBytesBE = new byte[8]; // holds counter as long to update for (int i = 0; i < DATA_SIZE_BYTE / HASH_LENGTH_BYTE; i++) { counterBytesBE[7] = (byte) i; // set first byte sha.update(counterBytesBE, 0, counterBytesBE.length); // BIG_ENDIAN_64(i) sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.doFinal(hashBuffer, 0); sha.reset(); // hash values allow weak garbage collector attack - // so, avoid new allocations: for (int j = 0; j < HASH_LENGTH_BYTE / 4; j++) { data[HASH_LENGTH_INT * i + j] = ((hashBuffer[j * 4 + 3] & 0xFF) << 24) | ((hashBuffer[j * 4 + 2] & 0xFF) << 16) | ((hashBuffer[j * 4 + 1] & 0xFF) << 8) | (hashBuffer[j * 4 + 0] & 0xFF); // little endian order } Arrays.fill(hashBuffer, (byte) 0); } // Initialize memory: for (int i = 0; i < memSize; i++) { // data = blowfish_encrypt_cbc(data) // mem = mem || data for (int j = 0; j < DATA_SIZE_INT; j++) { dataBuffer[j * 4 + 0] = (byte) (data[j]);// little endian dataBuffer[j * 4 + 1] = (byte) (data[j] >>> 8); dataBuffer[j * 4 + 2] = (byte) (data[j] >>> 16); dataBuffer[j * 4 + 3] = (byte) (data[j] >>> 24); } int len = cipher.processBytes(dataBuffer, 0, DATA_SIZE_BYTE, dataBuffer, 0); cipher.doFinal(dataBuffer, len); cipher.reset(); // get iv for next encryption step: // "running CBC": the last block of the // previous call is the IV for the next call System.arraycopy(dataBuffer, DATA_SIZE_BYTE - IV_LENGTH_BYTE, iv, 0, IV_LENGTH_BYTE); ivParams = new ParametersWithIV(params, iv); cipher.init(true, ivParams); for (int j = 0; j < DATA_SIZE_INT; j++) { data[j] = ((dataBuffer[j * 4 + 3] & 0xFF) << 24) | ((dataBuffer[j * 4 + 2] & 0xFF) << 16) | ((dataBuffer[j * 4 + 1] & 0xFF) << 8) | (dataBuffer[j * 4 + 0] & 0xFF); // little endian order } System.arraycopy(data, 0, mem, DATA_SIZE_INT * i, DATA_SIZE_INT); } // encrypt data: for (int j = 0; j < DATA_SIZE_INT; j++) { dataBuffer[j * 4 + 0] = (byte) (data[j]);// little endian dataBuffer[j * 4 + 1] = (byte) (data[j] >>> 8); dataBuffer[j * 4 + 2] = (byte) (data[j] >>> 16); dataBuffer[j * 4 + 3] = (byte) (data[j] >>> 24); } int len = cipher.processBytes(dataBuffer, 0, DATA_SIZE_BYTE, dataBuffer, 0); cipher.doFinal(dataBuffer, len); cipher.reset(); System.arraycopy(dataBuffer, DATA_SIZE_BYTE - IV_LENGTH_BYTE, iv, 0, IV_LENGTH_BYTE); ivParams = new ParametersWithIV(params, iv); cipher.init(true, ivParams); for (int j = 0; j < DATA_SIZE_INT; j++) { data[j] = ((dataBuffer[j * 4 + 3] & 0xFF) << 24) | ((dataBuffer[j * 4 + 2] & 0xFF) << 16) | ((dataBuffer[j * 4 + 1] & 0xFF) << 8) | (dataBuffer[j * 4 + 0] & 0xFF); // little endian order } // work: for (long i = 0; i < loops; i++) { for (int j = 0; j < memSize; j++) { // in the C++ reference implementation and the paper // this rValue a 64 bit integer, but this makes only a // difference for memSize > 0xFFFFFFFF +1, while the // recommended maximum for memSize is 2^32 int rValue = ((((int) data[DATA_SIZE_INT - 1]) << 24) & 0xff000000) | ((((int) data[DATA_SIZE_INT - 1]) << 8) & 0x00ff0000) | ((((int) data[DATA_SIZE_INT - 1]) >>> 8) & 0x0000ff00) | ((((int) data[DATA_SIZE_INT - 1]) >>> 24) & 0x000000ff); int index = (int) (DATA_SIZE_INT * (rValue & memMask)); for (int k = 0; k < DATA_SIZE_INT; k++) { mem[j * DATA_SIZE_INT + k] ^= data[k] ^ mem[index + k]; } // convert to byte: for (int k = 0; k < DATA_SIZE_INT; k++) { dataBuffer[k * 4 + 0] = (byte) (mem[j * DATA_SIZE_INT + k]); dataBuffer[k * 4 + 1] = (byte) (mem[j * DATA_SIZE_INT + k] >>> 8); dataBuffer[k * 4 + 2] = (byte) (mem[j * DATA_SIZE_INT + k] >>> 16); dataBuffer[k * 4 + 3] = (byte) (mem[j * DATA_SIZE_INT + k] >>> 24); } int len1 = cipher.processBytes(dataBuffer, 0, DATA_SIZE_BYTE, dataBuffer, 0); cipher.doFinal(dataBuffer, len1); cipher.reset(); // get iv for next step: System.arraycopy(dataBuffer, DATA_SIZE_BYTE - IV_LENGTH_BYTE, iv, 0, IV_LENGTH_BYTE); for (int k = 0; k < DATA_SIZE_INT; k++) { mem[j * DATA_SIZE_INT + k] = ((dataBuffer[k * 4 + 3] & 0xFF) << 24) | ((dataBuffer[k * 4 + 2] & 0xFF) << 16) | ((dataBuffer[k * 4 + 1] & 0xFF) << 8) | (dataBuffer[k * 4 + 0] & 0xFF); // little endian order } ivParams = new ParametersWithIV(params, iv); cipher.init(true, ivParams); // data ^= mem[j] for (int k = 0; k < DATA_SIZE_INT; k++) { data[k] ^= mem[DATA_SIZE_INT * j + k]; } } } // Finish // key = truncate(SHA512(SHA512(data || key)), outlen) || zeros(HASH_LENGTH - outlen) // convert to byte: for (int k = 0; k < DATA_SIZE_INT; k++) { dataBuffer[k * 4 + 0] = (byte) (data[k]); dataBuffer[k * 4 + 1] = (byte) (data[k] >>> 8); dataBuffer[k * 4 + 2] = (byte) (data[k] >>> 16); dataBuffer[k * 4 + 3] = (byte) (data[k] >>> 24); } sha.update(dataBuffer, 0, DATA_SIZE_BYTE); sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.doFinal(keyBytes, 0); sha.reset(); } sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.doFinal(keyBytes, 0); sha.reset(); byte[] out = new byte[outlen]; System.arraycopy(keyBytes, 0, out, 0, out.length); // Clean-up: Arrays.fill(keyBytes, (byte) 0); Arrays.fill(dataBuffer, (byte) 0); Arrays.fill(iv, (byte) 0); Arrays.fill(data, 0); Arrays.fill(mem, (byte) 0); // wipe the key from parameters Arrays.fill(params.getKey(), (byte) 0); // prevent dead code eliminations (compiler optimizations): if ((keyBytes[HASH_LENGTH_BYTE - 1] | blowfishKey[blowfishKey.length - 1] | dataBuffer[DATA_SIZE_BYTE - 1] | hashBuffer[HASH_LENGTH_BYTE - 1] | data[DATA_SIZE_INT - 1] | iv[IV_LENGTH_BYTE - 1] | mem[mem.length - 1] | params.getKey()[params.getKey().length - 1]) != 0) { System.err.print("zeroization failed!"); } if ((wipePassword == true) && (in[in.length - 1] != 0)) { System.err.print("zeroization failed!"); } return out; }
From source file:cologne.eck.peafactory.gui.Menu.java
License:Open Source License
@Override public void actionPerformed(ActionEvent ape) { //JComponent source = (JComponent) ape.getSource(); String command = ape.getActionCommand(); //Menu/*from w w w. j a v a 2 s . c o m*/ if (command.equals("newProject")) { ProjectSelection proj = new ProjectSelection(); Point p = MainView.getFrameLocation(); proj.setLocation((int) p.getX() + 100, (int) p.getY() + 60); proj.setVisible(true); } else if (command.equals("randomPassword")) { PasswordGeneratorDialog pg = new PasswordGeneratorDialog(PeaFactory.getFrame()); pg.setVisible(true); } else if (command.equals("keyboard")) { int input = JOptionPane.showConfirmDialog(PeaFactory.getFrame(), languageBundle.getString("add_keyboard"), " ", JOptionPane.YES_NO_OPTION); if (input == 0) { FileModifier.setSetKeyboard(true); } else { FileModifier.setSetKeyboard(false); } } else if (command.equals("psw_generator")) { int input = JOptionPane.showConfirmDialog(PeaFactory.getFrame(), languageBundle.getString("add_psw_generator"), " ", JOptionPane.YES_NO_OPTION); if (input == 0) { FileModifier.setPswGenerator(true); } else { FileModifier.setPswGenerator(false); } } else if (command.equals("quit")) { System.exit(0); } else if (command.equals("generalPeaSettings")) { @SuppressWarnings("unused") GeneralPeaSettings imageSetting = new GeneralPeaSettings(); } else if (command.equals("setThoughtless")) { securityLevel = 1; setSecurityLevel(1); } else if (command.equals("setLow")) { securityLevel = 2; setSecurityLevel(2); } else if (command.equals("setStandard")) { securityLevel = 3; setSecurityLevel(3); } else if (command.equals("setHigh")) { securityLevel = 4; setSecurityLevel(4); } else if (command.equals("setParanoid")) { securityLevel = 5; setSecurityLevel(5); } else if (command.equals("setBcrypt")) { setSecurityLevel(securityLevel); KeyDerivation.setKdf(new BcryptKDF()); } else if (command.equals("setScrypt")) { setSecurityLevel(securityLevel); KeyDerivation.setKdf(new ScryptKDF()); } else if (command.equals("setDragonfly")) { setSecurityLevel(securityLevel); CatenaKDF.setVersionID("Dragonfly-Full"); KeyDerivation.setKdf(new CatenaKDF()); } else if (command.equals("setButterfly")) { setSecurityLevel(securityLevel); CatenaKDF.setVersionID("Butterfly-Full"); KeyDerivation.setKdf(new CatenaKDF()); } else if (command.equals("setPomelo")) { setSecurityLevel(securityLevel); KeyDerivation.setKdf(new PomeloKDF()); } else if (command.equals("setBcryptParameters")) { @SuppressWarnings("unused") BcryptSetting bcryptSetting = new BcryptSetting(); } else if (command.equals("setPomeloParameters")) { @SuppressWarnings("unused") PomeloSetting pomeloSetting = new PomeloSetting(); } else if (command.equals("setScryptParameters")) { @SuppressWarnings("unused") ScryptSetting scryptSetting = new ScryptSetting(); } else if (command.equals("setCatenaParameters")) { @SuppressWarnings("unused") CatenaSetting catenaSetting = new CatenaSetting(); } else if (command.equals("setImageParameters")) { @SuppressWarnings("unused") ImageSetting imageSetting = new ImageSetting(); } else if (command.equals("setShacal2")) { CipherStuff.setCipherAlgo(new Shacal2Engine()); } else if (command.equals("setThreefish256")) { CipherStuff.setCipherAlgo(new ThreefishEngine(256)); } else if (command.equals("setThreefish512")) { CipherStuff.setCipherAlgo(new ThreefishEngine(512)); } else if (command.equals("setThreefish1024")) { CipherStuff.setCipherAlgo(new ThreefishEngine(1024)); } else if (command.equals("setTwofish")) { CipherStuff.setCipherAlgo(new TwofishEngine()); } else if (command.equals("setSerpent")) { CipherStuff.setCipherAlgo(new SerpentEngine()); } else if (command.equals("setAES")) { CipherStuff.setCipherAlgo(new AESEngine()); } else if (command.equals("setAESFast")) { CipherStuff.setCipherAlgo(new AESFastEngine()); // hash function: } else if (command.equals("setWhirlpool")) { HashStuff.setHashAlgo(new WhirlpoolDigest()); } else if (command.equals("setKeccak")) { HashStuff.setHashAlgo(new SHA3Digest()); } else if (command.equals("setSha512")) { HashStuff.setHashAlgo(new SHA512Digest()); } else if (command.equals("setSha384")) { HashStuff.setHashAlgo(new SHA384Digest()); } else if (command.equals("setSkein512")) { HashStuff.setHashAlgo(new SkeinDigest(512, 512)); } else if (command.equals("setBlake512")) { HashStuff.setHashAlgo(new Blake2bDigest()); // } else if (command.equals("setRipemd256")) { // HashStuff.setHashAlgo( new RIPEMD256Digest() ); } else if (command.equals("setRipemd320")) { HashStuff.setHashAlgo(new RIPEMD320Digest()); } else if (command.equals("setDE")) { PeaFactory.setI18n("de"); } else if (command.equals("setEN")) { PeaFactory.setI18n("en"); } else if (command.equals("notes")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("notes_description"), null, "notes"); } else if (command.equals("editor")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("editor_description"), null, "editor"); } else if (command.equals("image")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("image_description"), null, "image"); } else if (command.equals("keyboard_info")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog("Onscreen Keyboard", null, "keyboard"); } else if (command.equals("file")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("file_description"), null, "file"); } else if (command.equals("problemHelp")) { JOptionPane pane = new JOptionPane(languageBundle.getString("problem_help_dialog"), JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_OPTION, null, null);//new ImageIcon(PswDialogView.getImage()), null); pane.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12)); //pane.setIconImage(PswDialogView.getImage()); pane.setVisible(true); //pane.showMessageDialog(null, languageBundle.getString("problem_help_dialog"), null, JOptionPane.PLAIN_MESSAGE); } else if (command.equals("howToUse")) { JOptionPane.showMessageDialog(PeaFactory.getFrame(), languageBundle.getString("how_to_use_dialog"), null, JOptionPane.PLAIN_MESSAGE); } else if (command.equals("aboutLicense")) { JOptionPane.showMessageDialog(PeaFactory.getFrame(), languageBundle.getString("about_license_dialog"), null, JOptionPane.PLAIN_MESSAGE); } }
From source file:com.distrimind.util.crypto.P2PJPAKESecretMessageExchanger.java
License:Open Source License
P2PJPAKESecretMessageExchanger(AbstractSecureRandom secureRandom, byte[] participantID, char[] message, byte[] salt, int offset_salt, int len_salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { super(3, 3);//from w ww . j a v a2s . c om if (message == null) throw new NullPointerException("message"); if (salt != null && salt.length - offset_salt < len_salt) throw new IllegalArgumentException("salt"); jpake = new JPAKEParticipant(getParticipanIDString(participantID), getHashedPassword(message, salt, offset_salt, len_salt), JPAKEPrimeOrderGroups.NIST_3072, new SHA512Digest(), secureRandom); this.keyMaterial = null; }
From source file:com.distrimind.util.crypto.P2PJPAKESecretMessageExchanger.java
License:Open Source License
P2PJPAKESecretMessageExchanger(AbstractSecureRandom secureRandom, byte[] participantID, byte[] message, int offset, int len, byte[] salt, int offset_salt, int len_salt, boolean messageIsKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { super(3, 3);//from w w w .j ava2 s . c o m if (message == null) throw new NullPointerException("message"); if (message.length - offset < len) throw new IllegalArgumentException("message"); if (salt != null && salt.length - offset_salt < len_salt) throw new IllegalArgumentException("salt"); jpake = new JPAKEParticipant(getParticipanIDString(participantID), getHashedPassword(message, offset, len, salt, offset_salt, len_salt, messageIsKey), JPAKEPrimeOrderGroups.NIST_3072, new SHA512Digest(), secureRandom); this.keyMaterial = null; }
From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java
License:Open Source License
protected ParametersWithIV createDecryptionParameters(String password, String salt, byte[] iv) { byte[] passwordBytes = password.getBytes(Charsets.UTF_8); byte[] saltBytes = salt.getBytes(Charsets.UTF_8); PKCS5S1ParametersGenerator keyGenerator = new PKCS5S1ParametersGenerator(new SHA512Digest()); keyGenerator.init(passwordBytes, saltBytes, PBE_ITERATION_COUNT); KeyParameter params = (KeyParameter) keyGenerator.generateDerivedParameters(KEY_SIZE); return new ParametersWithIV(params, iv); }
From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java
License:Open Source License
protected ParametersWithIV createEncryptionParameters(String password, String salt) { byte[] passwordBytes = password.getBytes(Charsets.UTF_8); byte[] saltBytes = salt.getBytes(Charsets.UTF_8); PKCS5S1ParametersGenerator keyGenerator = new PKCS5S1ParametersGenerator(new SHA512Digest()); keyGenerator.init(passwordBytes, saltBytes, PBE_ITERATION_COUNT); ParametersWithIV params = (ParametersWithIV) keyGenerator.generateDerivedParameters(KEY_SIZE, IV_SIZE); return params; }
From source file:com.github.horrorho.inflatabledonkey.crypto.key.SignatureAssistant.java
License:Open Source License
public static Optional<Digest> digestForSignature(Signature signature) { switch (signature.type()) { case 0x01:/*from w w w . ja v a2 s . com*/ return Optional.ofNullable(new SHA256Digest()); case 0x02: return Optional.ofNullable(new SHA512Digest()); default: logger.warn("-- digestForSignature() - unsupported signature type: {}", signature); return Optional.empty(); } }
From source file:com.github.horrorho.inflatabledonkey.pcs.key.SignatureAssistant.java
License:Open Source License
static Optional<Digest> digest(Signature signature) { switch (signature.type()) { case 0x01://w w w.ja v a 2 s . c om return Optional.ofNullable(new SHA256Digest()); case 0x02: return Optional.ofNullable(new SHA512Digest()); default: logger.warn("-- digest() - unsupported signature type: {}", signature); return Optional.empty(); } }