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:io.netty.example.ocsp.Digester.java

License:Apache License

public static DigestCalculator sha1() {
    Digest digest = new SHA1Digest();
    AlgorithmIdentifier algId = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1);

    return new Digester(digest, algId);
}

From source file:it.yup.util.Utils.java

/**
 * //from   w  w w  .j  av  a2 s .c om
 * @param data
 * @param digestType
 * @return the digest or null if the requested digest is not supported
 */
static public byte[] digest(byte data[], String digestType) {
    GeneralDigest digest = null;
    if (digestType.equals("sha1")) {
        digest = new SHA1Digest();
    } else if (digestType.equals("md5")) {
        digest = new MD5Digest();
    } else {
        return null;
    }

    // XXX too many copies of data, modify the hash functions so that they write
    // the result to a byte array
    digest.update(data, 0, data.length);
    // some emulators fail on calling getByteLength  
    byte out[] = null;
    try {
        out = new byte[digest.getByteLength()];
    } catch (Error e) {
        out = new byte[64];
    }

    int len = digest.doFinal(out, 0);
    byte result[] = new byte[len];
    System.arraycopy(out, 0, result, 0, len);
    return result;
}

From source file:jazmin.server.relay.udp.webrtc.SRTCPCryptoContext.java

License:LGPL

/**
 * Construct a normal SRTPCryptoContext based on the given parameters.
 * // w w  w . j  a v a2  s  .  co  m
 * @param ssrc
 *            the RTP SSRC that this SRTP cryptographic context protects.
 * @param masterKey
 *            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 masterSalt
 *            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 policy
 *            SRTP policy for this SRTP cryptographic context, defined the
 *            encryption algorithm, the authentication algorithm, etc
 */
@SuppressWarnings("fallthrough")
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, masterK.length);
    masterSalt = new byte[policy.getSaltKeyLength()];
    System.arraycopy(masterS, 0, masterSalt, 0, masterS.length);

    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:
        authKey = new byte[policy.getAuthKeyLength()];
        tagStore = new byte[policy.getAuthTagLength()];
        break;

    default:
        tagStore = null;
    }
}

From source file:jazmin.server.relay.udp.webrtc.SRTPCryptoContext.java

License:LGPL

/**
 * Construct a normal SRTPCryptoContext based on the given parameters.
 * /*from  w ww.ja  v  a 2s  . 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
 */
@SuppressWarnings("fallthrough")
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, masterK.length);

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

    mac = new HMac(new SHA1Digest());

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

    case SRTPPolicy.AESF8_ENCRYPTION:
        cipherF8 = new AESFastEngine();
        //$FALL-THROUGH$

    case SRTPPolicy.AESCM_ENCRYPTION:
        cipher = new AESFastEngine();
        encKey = new byte[policy.getEncKeyLength()];
        saltKey = new byte[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;

    default:
        tagStore = null;
    }
}

From source file:jazmin.server.relay.udp.webrtc.TlsUtils.java

License:Open Source License

static byte[] digestOf(String hashFunction, byte[] input) {
    Digest d;// w w  w. j a v a 2s.  c o  m
    switch (hashFunction) {
    case SHA_1:
        d = new SHA1Digest();
        break;

    case SHA_512:
        d = new SHA512Digest();
        break;

    case SHA_256:
    default:
        d = new SHA256Digest();
        break;
    }

    d.update(input, 0, input.length);
    byte[] result = new byte[d.getDigestSize()];
    d.doFinal(result, 0);
    return result;
}

From source file:net.java.sip.communicator.impl.neomedia.transform.srtp.SRTCPCryptoContext.java

License:LGPL

/**
 * Construct a normal SRTPCryptoContext based on the given parameters.
 * /*from   ww  w.jav a  2  s  .co m*/
 * @param ssrc
 *            the RTP SSRC that this SRTP cryptographic context protects.
 * @param masterKey
 *            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 masterSalt
 *            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 policy
 *            SRTP policy for this SRTP cryptographic context, defined the
 *            encryption algorithm, the authentication algorithm, etc
 */
@SuppressWarnings("fallthrough")
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:net.java.sip.communicator.impl.neomedia.transform.srtp.SRTPCryptoContext.java

License:LGPL

/**
 * Construct a normal SRTPCryptoContext based on the given parameters.
 *
 * @param ssrcIn//ww w.  j av a 2s. co m
 *            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
 */
@SuppressWarnings("fallthrough")
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());

    mac = new HMac(new SHA1Digest());

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

    case SRTPPolicy.AESF8_ENCRYPTION:
        cipherF8 = new AESFastEngine();
        //$FALL-THROUGH$

    case SRTPPolicy.AESCM_ENCRYPTION:
        cipher = new AESFastEngine();
        encKey = new byte[policy.getEncKeyLength()];
        saltKey = new byte[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:net.nharyes.secrete.curve.TestCurve25519PrivateKey.java

License:Open Source License

@Test
public void testPBKDF() throws Exception {

    Security.addProvider(new BouncyCastleProvider());

    Random r = new Random();

    char[] cPassword = "ThePa55wordToU5e".toCharArray();
    byte[] salt = new byte[64];
    r.nextBytes(salt);//ww  w. ja va  2s .  co  m

    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keyspec = new PBEKeySpec(cPassword, salt, 5000, 256);
    Key key = factory.generateSecret(keyspec);
    byte[] k1 = key.getEncoded();

    factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "BC");
    key = factory.generateSecret(keyspec);
    byte[] k2 = key.getEncoded();

    assertArrayEquals(k1, k2);

    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA1Digest());
    gen.init(new String(cPassword).getBytes("UTF-8"), salt, 5000);
    byte[] k3 = ((KeyParameter) gen.generateDerivedParameters(256)).getKey();

    assertArrayEquals(k1, k3);
    assertArrayEquals(k2, k3);
}

From source file:net.oauth.j2me.signature.HMACSHA1Signature.java

License:Apache License

public String getSignature() {
    try {//from w ww. ja va2s . c o m
        HMac m = new HMac(new SHA1Digest());
        m.init(new KeyParameter(key.getBytes("UTF-8")));
        byte[] bytes = message.getBytes("UTF-8");
        m.update(bytes, 0, bytes.length);
        byte[] mac = new byte[m.getMacSize()];
        m.doFinal(mac, 0);
        signature = new String(Util.base64Encode(mac));

        // debug
        System.out.println("mac alg: " + m.getAlgorithmName());
        System.out.println("dig alg: " + m.getUnderlyingDigest().getAlgorithmName());
        System.out.println("key: " + key);
        System.out.println("message: " + message);
        System.out.println("unencoded: " + new String(mac));
        System.out.println("sig: " + signature);
    } catch (java.io.UnsupportedEncodingException e) {
        ;
    } catch (Exception e) {
        ;
    }

    return signature;
}

From source file:net.propero.rdp.Secure.java

License:Open Source License

/**
 * Initialise Secure layer of communications
 *
 * @param channels// ww  w.j  ava 2 s .com
 *            Virtual channels for this connection
 * @param options
 *            The options instance
 */
public Secure(VChannels channels, Options options, Rdp rdp) {
    this.channels = channels;
    this.options = options;
    this.licence = new Licence(options, this);
    McsLayer = new MCS(options, channels, this, rdp);
    rc4_dec = new RC4Engine();
    rc4_enc = new RC4Engine();
    rc4_update = new RC4Engine();
    sha1 = new SHA1Digest();
    md5 = new MD5Digest();
    sec_sign_key = new byte[16]; // changed from 8 - rdesktop 1.2.0
    sec_decrypt_key = new byte[16];
    sec_encrypt_key = new byte[16];
    sec_decrypt_update_key = new byte[16]; // changed from 8 - rdesktop
    // 1.2.0
    sec_encrypt_update_key = new byte[16]; // changed from 8 - rdesktop
    // 1.2.0
    sec_crypted_random = new byte[64];

}