use of org.bouncycastle.crypto.params.KeyParameter in project web3sdk by FISCO-BCOS.
the class Wallet method generateAes128CtrDerivedKey.
private static byte[] generateAes128CtrDerivedKey(byte[] password, byte[] salt, int c, String prf) throws CipherException {
if (!prf.equals("hmac-sha256")) {
throw new CipherException("Unsupported prf:" + prf);
}
// Java 8 supports this, but you have to convert the password to a character array, see
// http://stackoverflow.com/a/27928435/3211687
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
gen.init(password, salt, c);
return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
use of org.bouncycastle.crypto.params.KeyParameter in project web3sdk by FISCO-BCOS.
the class Hash method hmacSha512.
public static byte[] hmacSha512(byte[] key, byte[] input) {
HMac hMac = new HMac(new SHA512Digest());
hMac.init(new KeyParameter(key));
hMac.update(input, 0, input.length);
byte[] out = new byte[64];
hMac.doFinal(out, 0);
return out;
}
use of org.bouncycastle.crypto.params.KeyParameter in project Conversations by siacs.
the class ScramMechanism method hmac.
private byte[] hmac(final byte[] key, final byte[] input) throws InvalidKeyException {
final HMac hMac = getHMAC();
hMac.init(new KeyParameter(key));
hMac.update(input, 0, input.length);
final byte[] out = new byte[hMac.getMacSize()];
hMac.doFinal(out, 0);
return out;
}
use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.
the class SkeinTest method checkKATVectors.
boolean checkKATVectors() {
KatResult kr = new KatResult();
ParametersForSkein pfs;
while (scanner.fillResult(kr)) {
// Skip Tree vectors in this test function
if (kr.restOfLine.contains("Tree")) {
notProcessed++;
continue;
}
if (kr.restOfLine.contains("MAC")) {
pfs = new ParametersForSkein(new KeyParameter(kr.macKey), kr.stateSize, kr.hashBitLength);
SkeinMac sm = new SkeinMac();
sm.init(pfs);
sm.updateBits(kr.msg, 0, kr.msgLength);
byte[] mac = new byte[sm.getMacSize()];
sm.doFinal(mac, 0);
if (!Arrays.equals(mac, kr.result)) {
System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-" + kr.msgLength + "-" + kr.restOfLine);
hexdump("Computed mac", mac, mac.length);
hexdump("Expected result", kr.result, kr.result.length);
return false;
}
processed++;
continue;
}
Skein skein = new Skein(kr.stateSize, kr.hashBitLength);
skein.updateBits(kr.msg, 0, kr.msgLength);
byte[] hash = skein.doFinal();
if (!Arrays.equals(hash, kr.result)) {
System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-" + kr.msgLength + "-" + kr.restOfLine);
hexdump("Computed hash", hash, hash.length);
hexdump("Expected result", kr.result, kr.result.length);
return false;
}
// Enable the next few line so you can check some results manually
// if ((kr.msgLength & 1) == 1) {
// System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-"
// + kr.msgLength + "-" + kr.restOfLine);
// hexdump("Computed hash", hash, hash.length);
// hexdump("Expected result", kr.result, kr.result.length);
// }
processed++;
}
return true;
}
use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.
the class ThreefishCipher method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
if (params instanceof ParametersForThreefish) {
ParametersForThreefish pft = (ParametersForThreefish) params;
stateSize = pft.getStateSize();
setCipher(stateSize);
if (cipher == null)
throw new IllegalArgumentException("Threefish: unsupported state size: " + stateSize);
byte[] key = ((KeyParameter) pft.getParameters()).getKey();
if (key.length != (stateSize / 8))
throw new IllegalArgumentException("Threefish: key length does not match state size: " + key.length);
long[] tweak = pft.getTweak();
if (tweak == null)
throw new IllegalArgumentException("Threefish: tweak data not set");
cipher.setTweak(tweak);
// Get a long array for cipher key and moves the byte key buffer to it
long[] keyLong = new long[stateSize / 64];
for (int i = 0; i < keyLong.length; i++) keyLong[i] = ByteLong.GetUInt64(key, i * 8);
cipher.setKey(keyLong);
this.forEncryption = forEncryption;
// Allocate buffers
cipherIn = new long[stateSize / 64];
cipherOut = new long[stateSize / 64];
return;
}
throw new IllegalArgumentException("Threfish: invalid parameter passed to init - " + params.getClass().getName());
}
Aggregations