Example usage for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest

List of usage examples for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest.

Prototype

public SHA1Digest() 

Source Link

Document

Standard constructor

Usage

From source file:eu.betaas.taas.securitymanager.common.mqv.ECMQVUtils.java

License:Apache License

/**
 * A method to derive a new key using HKDF (Hash based Key Derivation Function) 
 * @param keyIn: an "original" key to be derived
 * @param keyOutLenByte: the length of the new derived (output) key in byte
 * @return // www .jav  a2 s  .  com
 */
public static byte[] deriveKeyHKDF(byte[] keyIn, int keyOutLenByte) {
    DerivationParameters kdfParam = new HKDFParameters(keyIn, null, intToByteArray(keyOutLenByte * 8));

    HKDFBytesGenerator hkdfGen = new HKDFBytesGenerator(new SHA1Digest());
    hkdfGen.init(kdfParam);
    // initialize the new key with size of L bits (or L/8 bytes)
    byte[] newKey = new byte[keyOutLenByte];

    hkdfGen.generateBytes(newKey, 0, keyOutLenByte);

    return newKey;
}

From source file:eu.betaas.taas.securitymanager.common.mqv.ECMQVUtils.java

License:Apache License

/**
 * A method to calculate the MAC to be sent to other GW in the ECMQV process
 * @param num: an integer that represents the step, e.g. either 2 or 3
 * @param ufnA: User Friendly Name of GW A
 * @param ufnB: User Friendly Name of GW B
 * @param ephPubA: Ephemeral public key of GW A
 * @param ephPubB: Ephemeral public key of GW A
 * @param k1: key to encrypt the MAC (derived from KDF)
 * @return//w  ww  .j  a v  a2  s  . co m
 */
public static byte[] computeMAC(String num, String ufnA, String ufnB, byte[] ephPubA, byte[] ephPubB,
        byte[] k1) {

    HMac hmac = new HMac(new SHA1Digest());
    hmac.init(new KeyParameter(k1));

    // concatenate the message/info (in bytes)
    byte[] numByte = num.getBytes();
    byte[] ufnAbyte = ufnA.getBytes();
    byte[] ufnBbyte = ufnB.getBytes();
    int byteLen = numByte.length + ufnAbyte.length + ufnBbyte.length + ephPubA.length + ephPubB.length;
    byte[] in = new byte[byteLen];

    int c = 0;
    for (int i = 0; i < numByte.length; i++) {
        in[c] = numByte[i];
        c++;
    }
    for (int i = 0; i < ufnAbyte.length; i++) {
        in[c] = ufnAbyte[i];
        c++;
    }
    for (int i = 0; i < ufnBbyte.length; i++) {
        in[c] = ufnBbyte[i];
        c++;
    }
    for (int i = 0; i < ephPubA.length; i++) {
        in[c] = ephPubA[i];
        c++;
    }
    for (int i = 0; i < ephPubB.length; i++) {
        in[c] = ephPubB[i];
        c++;
    }

    hmac.update(in, 0, byteLen);

    byte[] out = new byte[hmac.getMacSize()];
    hmac.doFinal(out, 0);

    return out;
}

From source file:fc.xml.xas.security.SecUtil.java

License:Open Source License

public static Digest getDigest(String id) {
    Verifier.checkNotNull(id);//from   w ww  .  jav  a 2  s  .  c om
    if (id.equals(SHA_1_DIGEST)) {
        return new SHA1Digest();
    } else {
        return null;
    }
}

From source file:fc.xml.xas.security.SecUtil.java

License:Open Source License

public static Digest getDigestFromSignature(String id) {
    Verifier.checkNotNull(id);//from w w w.  j  a va2  s.com
    if (id.equals(RSA_SIGNATURE)) {
        return new SHA1Digest();
    } else {
        return null;
    }
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

public FrostCrypt() {
    Security.addProvider(new BouncyCastleProvider());

    signer = new PSSSigner(new RSAEngine(), new SHA1Digest(), 16);
    try {/*from  w  w  w. j  a  v  a2  s  . c  om*/
        secureRandom = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        secureRandom = new SecureRandom();
    }
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

/**
 * Computes the SHA-1 checksum of given message.
 *//*w  w w.  j a va2s.co  m*/
public synchronized String digest(String message) {
    try {
        SHA1Digest stomach = new SHA1Digest();
        stomach.reset();
        byte[] food = message.getBytes("UTF-8");
        stomach.update(food, 0, food.length);
        byte[] poop = new byte[64];
        stomach.doFinal(poop, 0);
        return (new String(Base64.encode(poop))).substring(0, 27);
    } catch (UnsupportedEncodingException ex) {
        Logger.error(this, "UTF-8 encoding is not supported : " + ex.toString());
    }
    return null;
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

/**
 * Computes the SHA-1 checksum of given file.
 *//*from   w  w w. jav  a 2  s.co m*/
public synchronized String digest(File file) {
    SHA1Digest stomach = new SHA1Digest();
    byte[] poop = new byte[64];
    FileChannel chan = null;
    try {
        chan = (new FileInputStream(file)).getChannel();
    } catch (IOException e) {
        Logger.error(this, "Exception thrown in digest(File file): " + e.toString());
    }
    byte[] temp = new byte[4 * 1024];
    ByteBuffer _temp = ByteBuffer.wrap(temp);
    try {
        while (true) {
            //if (y >= file.length()) break;
            //if (y > file.length()) y = file.length();
            int pos = _temp.position();
            int read = chan.read(_temp);
            if (read == -1)
                break;
            stomach.update(temp, pos, read);
            if (_temp.remaining() == 0)
                _temp.position(0);
        }
        chan.close();
    } catch (IOException e) {
        Logger.error(this, "Exception thrown in digest(File file): " + e.toString());
    }
    stomach.doFinal(poop, 0);
    return (new String(Base64.encode(poop))).substring(0, 27);
}

From source file:gnu.java.zrtp.jmf.transform.srtp.SRTCPCryptoContext.java

License:LGPL

/**
 * Construct a normal SRTPCryptoContext based on the given parameters.
 * /*  w  ww .  j a  v  a2 s.c  o m*/
 * @param ssrcIn
 *            the RTP SSRC that this SRTP cryptographic context protects.
 * @param masterK
 *            byte array holding the master key for this SRTP cryptographic
 *            context. Refer to chapter 3.2.1 of the RFC about the role of
 *            the master key.
 * @param masterS
 *            byte array holding the master salt for this SRTP cryptographic
 *            context. It is used to computer the initialization vector that
 *            in turn is input to compute the session key, session
 *            authentication key and the session salt.
 * @param policyIn
 *            SRTP policy for this SRTP cryptographic context, defined the
 *            encryption algorithm, the authentication algorithm, etc
 */
public SRTCPCryptoContext(long ssrcIn, byte[] masterK, byte[] masterS, SRTPPolicy policyIn) {
    ssrcCtx = ssrcIn;
    mki = null;

    policy = policyIn;

    masterKey = new byte[policy.getEncKeyLength()];
    System.arraycopy(masterK, 0, masterKey, 0, policy.getEncKeyLength());

    masterSalt = new byte[policy.getSaltKeyLength()];
    System.arraycopy(masterS, 0, masterSalt, 0, policy.getSaltKeyLength());

    switch (policy.getEncType()) {
    case SRTPPolicy.NULL_ENCRYPTION:
        encKey = null;
        saltKey = null;
        break;

    case SRTPPolicy.AESF8_ENCRYPTION:
        cipherF8 = new AESFastEngine();

    case SRTPPolicy.AESCM_ENCRYPTION:
        cipher = new AESFastEngine();
        encKey = new byte[this.policy.getEncKeyLength()];
        saltKey = new byte[this.policy.getSaltKeyLength()];
        break;

    case SRTPPolicy.TWOFISHF8_ENCRYPTION:
        cipherF8 = new TwofishEngine();

    case SRTPPolicy.TWOFISH_ENCRYPTION:
        cipher = new TwofishEngine();
        encKey = new byte[this.policy.getEncKeyLength()];
        saltKey = new byte[this.policy.getSaltKeyLength()];
        break;
    }

    switch (policy.getAuthType()) {
    case SRTPPolicy.NULL_AUTHENTICATION:
        authKey = null;
        tagStore = null;
        break;

    case SRTPPolicy.HMACSHA1_AUTHENTICATION:
        mac = new HMac(new SHA1Digest());
        authKey = new byte[policy.getAuthKeyLength()];
        tagStore = new byte[mac.getMacSize()];
        break;

    case SRTPPolicy.SKEIN_AUTHENTICATION:
        mac = new SkeinMac();
        authKey = new byte[policy.getAuthKeyLength()];
        tagStore = new byte[policy.getAuthTagLength()];
        break;

    default:
        tagStore = null;
    }
}

From source file:gnu.java.zrtp.jmf.transform.srtp.SRTPCryptoContext.java

License:LGPL

/**
 * Construct a normal SRTPCryptoContext based on the given parameters.
 * /*w  ww .ja v a2 s .c o m*/
 * @param ssrcIn
 *            the RTP SSRC that this SRTP cryptographic context protects.
 * @param rocIn
 *            the initial Roll-Over-Counter according to RFC 3711. These are
 *            the upper 32 bit of the overall 48 bit SRTP packet index.
 *            Refer to chapter 3.2.1 of the RFC.
 * @param kdr
 *            the key derivation rate defines when to recompute the SRTP
 *            session keys. Refer to chapter 4.3.1 in the RFC.
 * @param masterK
 *            byte array holding the master key for this SRTP cryptographic
 *            context. Refer to chapter 3.2.1 of the RFC about the role of
 *            the master key.
 * @param masterS
 *            byte array holding the master salt for this SRTP cryptographic
 *            context. It is used to computer the initialization vector that
 *            in turn is input to compute the session key, session
 *            authentication key and the session salt.
 * @param policyIn
 *            SRTP policy for this SRTP cryptographic context, defined the
 *            encryption algorithm, the authentication algorithm, etc
 */
public SRTPCryptoContext(long ssrcIn, int rocIn, long kdr, byte[] masterK, byte[] masterS,
        SRTPPolicy policyIn) {
    ssrcCtx = ssrcIn;
    mki = null;
    roc = rocIn;
    guessedROC = 0;
    seqNum = 0;
    keyDerivationRate = kdr;
    seqNumSet = false;

    policy = policyIn;

    masterKey = new byte[policy.getEncKeyLength()];
    System.arraycopy(masterK, 0, masterKey, 0, policy.getEncKeyLength());

    masterSalt = new byte[policy.getSaltKeyLength()];
    System.arraycopy(masterS, 0, masterSalt, 0, policy.getSaltKeyLength());

    switch (policy.getEncType()) {
    case SRTPPolicy.NULL_ENCRYPTION:
        encKey = null;
        saltKey = null;
        break;

    case SRTPPolicy.AESF8_ENCRYPTION:
        cipherF8 = new AESFastEngine();

    case SRTPPolicy.AESCM_ENCRYPTION:
        cipher = new AESFastEngine();
        encKey = new byte[this.policy.getEncKeyLength()];
        saltKey = new byte[this.policy.getSaltKeyLength()];
        break;

    case SRTPPolicy.TWOFISHF8_ENCRYPTION:
        cipherF8 = new TwofishEngine();

    case SRTPPolicy.TWOFISH_ENCRYPTION:
        cipher = new TwofishEngine();
        encKey = new byte[this.policy.getEncKeyLength()];
        saltKey = new byte[this.policy.getSaltKeyLength()];
        break;
    }

    switch (policy.getAuthType()) {
    case SRTPPolicy.NULL_AUTHENTICATION:
        authKey = null;
        tagStore = null;
        break;

    case SRTPPolicy.HMACSHA1_AUTHENTICATION:
        mac = new HMac(new SHA1Digest());
        authKey = new byte[policy.getAuthKeyLength()];
        tagStore = new byte[mac.getMacSize()];
        break;

    case SRTPPolicy.SKEIN_AUTHENTICATION:
        mac = new SkeinMac();
        authKey = new byte[policy.getAuthKeyLength()];
        tagStore = new byte[policy.getAuthTagLength()];
        break;

    default:
        tagStore = null;
    }
}

From source file:hu.akarnokd.utils.crypto.CryptoUtils.java

License:Apache License

/**
 * Generates salt with the given length.
 * @param size the number of bytes/*from   www. jav  a 2 s  .  c  o m*/
 * @return the salt bytes
 */
@NonNull
public static byte[] generateSalt(int size) {
    Digest digest = null;

    switch (String.format(DEFAULT_SALT_ALG, DEFAULT_PBE_KEY_BITS)) {
    case "SHA224PRNG":
        digest = new SHA224Digest();
        break;
    case "SHA256PRNG":
        digest = new SHA256Digest();
        break;
    case "SHA384PRNG":
        digest = new SHA384Digest();
        break;
    case "SHA512PRNG":
        digest = new SHA512Digest();
        break;
    default:
        digest = new SHA1Digest();
    }

    DigestRandomGenerator drg = new DigestRandomGenerator(digest);
    drg.addSeedMaterial(System.currentTimeMillis());
    byte[] r = new byte[size];
    drg.nextBytes(r);
    return r;
}