当前位置: 首页>>代码示例>>Java>>正文


Java JCAUtil.getSecureRandom方法代码示例

本文整理汇总了Java中sun.security.jca.JCAUtil.getSecureRandom方法的典型用法代码示例。如果您正苦于以下问题:Java JCAUtil.getSecureRandom方法的具体用法?Java JCAUtil.getSecureRandom怎么用?Java JCAUtil.getSecureRandom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sun.security.jca.JCAUtil的用法示例。


在下文中一共展示了JCAUtil.getSecureRandom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateKeyPair

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
/**
 * Generates a pair of keys usable by any JavaSecurity compliant
 * DSA implementation.
 */
public KeyPair generateKeyPair() {
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    DSAParameterSpec spec;
    try {
        if (forceNewParameters) {
            // generate new parameters each time
            spec = ParameterCache.getNewDSAParameterSpec(plen, qlen, random);
        } else {
            if (params == null) {
                params =
                    ParameterCache.getDSAParameterSpec(plen, qlen, random);
            }
            spec = params;
        }
    } catch (GeneralSecurityException e) {
        throw new ProviderException(e);
    }
    return generateKeyPair(spec.getP(), spec.getQ(), spec.getG(), random);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:26,代码来源:DSAKeyPairGenerator.java

示例2: engineSign

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
@Override
protected byte[] engineSign() throws SignatureException {
    byte[] s = privateKey.getS().toByteArray();
    ECParameterSpec params = privateKey.getParams();
    // DER OID
    byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params);
    int keySize = params.getCurve().getField().getFieldSize();

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    random.nextBytes(seed);

    try {

        return encodeSignature(
            signDigest(getDigestValue(), s, encodedParams, seed));

    } catch (GeneralSecurityException e) {
        throw new SignatureException("Could not sign data", e);
    }
}
 
开发者ID:JetBrains,项目名称:jdk8u_jdk,代码行数:25,代码来源:ECDSASignature.java

示例3: engineSign

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
@Override
protected byte[] engineSign() throws SignatureException {
    byte[] s = privateKey.getS().toByteArray();
    ECParameterSpec params = privateKey.getParams();
    byte[] encodedParams = ECParameters.encodeParameters(params); // DER OID
    int keySize = params.getCurve().getField().getFieldSize();

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    random.nextBytes(seed);

    try {

        return encodeSignature(
            signDigest(getDigestValue(), s, encodedParams, seed));

    } catch (GeneralSecurityException e) {
        throw new SignatureException("Could not sign data", e);
    }
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:24,代码来源:ECDSASignature.java

示例4: generateKeyPair

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
/**
 * Generates a pair of keys usable by any JavaSecurity compliant
 * DSA implementation.
 */
public KeyPair generateKeyPair() {
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    DSAParameterSpec spec;
    try {
        if (forceNewParameters) {
            // generate new parameters each time
            spec = ParameterCache.getNewDSAParameterSpec(modlen, random);
        } else {
            if (params == null) {
                params =
                    ParameterCache.getDSAParameterSpec(modlen, random);
            }
            spec = params;
        }
    } catch (GeneralSecurityException e) {
        throw new ProviderException(e);
    }
    return generateKeyPair(spec.getP(), spec.getQ(), spec.getG(), random);
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:26,代码来源:DSAKeyPairGenerator.java

示例5: padV15

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
/**
 * PKCS#1 v1.5 padding (blocktype 1 and 2).
 */
private byte[] padV15(byte[] data) throws BadPaddingException {
    byte[] padded = new byte[paddedSize];
    System.arraycopy(data, 0, padded, paddedSize - data.length,
        data.length);
    int psSize = paddedSize - 3 - data.length;
    int k = 0;
    padded[k++] = 0;
    padded[k++] = (byte)type;
    if (type == PAD_BLOCKTYPE_1) {
        // blocktype 1: all padding bytes are 0xff
        while (psSize-- > 0) {
            padded[k++] = (byte)0xff;
        }
    } else {
        // blocktype 2: padding bytes are random non-zero bytes
        if (random == null) {
            random = JCAUtil.getSecureRandom();
        }
        // generate non-zero padding bytes
        // use a buffer to reduce calls to SecureRandom
        byte[] r = new byte[64];
        int i = -1;
        while (psSize-- > 0) {
            int b;
            do {
                if (i < 0) {
                    random.nextBytes(r);
                    i = r.length - 1;
                }
                b = r[i--] & 0xff;
            } while (b == 0);
            padded[k++] = (byte)b;
        }
    }
    return padded;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:40,代码来源:RSAPadding.java

示例6: getSigningRandom

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
protected SecureRandom getSigningRandom() {
    if (signingRandom == null) {
        if (appRandom != null) {
            signingRandom = appRandom;
        } else {
            signingRandom = JCAUtil.getSecureRandom();
        }
    }
    return signingRandom;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:DSA.java

示例7: padV15

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
/**
 * PKCS#1 v1.5 padding (blocktype 1 and 2).
 */
private byte[] padV15(byte[] data) throws BadPaddingException {
    byte[] padded = new byte[paddedSize];
    System.arraycopy(data, 0, padded, paddedSize - data.length,
        data.length);
    int psSize = paddedSize - 3 - data.length;
    int k = 0;
    padded[k++] = 0;
    padded[k++] = (byte)type;
    if (type == PAD_BLOCKTYPE_1) {
        // blocktype 1: all padding bytes are 0xff
        while (psSize-- > 0) {
            padded[k++] = (byte)0xff;
        }
    } else {
        // blocktype 2: padding bytes are random non-zero bytes
        if (random == null) {
            random = JCAUtil.getSecureRandom();
        }
        // generate non-zero padding bytes
        // use a buffer to reduce calls to SecureRandom
        while (psSize > 0) {
            // extra bytes to avoid zero bytes,
            // number of zero bytes <= 4 in 98% cases
            byte[] r = new byte[psSize + 4];
            random.nextBytes(r);
            for (int i = 0; i < r.length && psSize > 0; i++) {
                if (r[i] != 0) {
                    padded[k++] = r[i];
                    psSize--;
                }
            }
        }
    }
    return padded;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:RSAPadding.java

示例8: getTokenId

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
private synchronized byte[] getTokenId() {
    if (tokenId == null) {
        SecureRandom random = JCAUtil.getSecureRandom();
        tokenId = new byte[20];
        random.nextBytes(tokenId);
        serializedTokens.add(new WeakReference<Token>(this));
    }
    return tokenId;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:Token.java

示例9: generateKeyPair

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
@Override
public KeyPair generateKeyPair() {

    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(null, (ECParameterSpec)params);

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    random.nextBytes(seed);

    try {

        Object[] keyBytes = generateECKeyPair(keySize, encodedParams, seed);

        // The 'params' object supplied above is equivalent to the native
        // one so there is no need to fetch it.
        // keyBytes[0] is the encoding of the native private key
        BigInteger s = new BigInteger(1, (byte[])keyBytes[0]);

        PrivateKey privateKey =
            new ECPrivateKeyImpl(s, (ECParameterSpec)params);

        // keyBytes[1] is the encoding of the native public key
        ECPoint w = ECUtil.decodePoint((byte[])keyBytes[1],
            ((ECParameterSpec)params).getCurve());
        PublicKey publicKey =
            new ECPublicKeyImpl(w, (ECParameterSpec)params);

        return new KeyPair(publicKey, privateKey);

    } catch (Exception e) {
        throw new ProviderException(e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:ECKeyPairGenerator.java

示例10: supportsRawSecretKeyImport

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
boolean supportsRawSecretKeyImport() {
    if (supportsRawSecretKeyImport == null) {
        SecureRandom random = JCAUtil.getSecureRandom();
        byte[] encoded = new byte[48];
        random.nextBytes(encoded);

        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[3];
        attributes[0] = new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY);
        attributes[1] = new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET);
        attributes[2] = new CK_ATTRIBUTE(CKA_VALUE, encoded);

        Session session = null;
        try {
            attributes = getAttributes(O_IMPORT,
                    CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes);
            session = getObjSession();
            long keyID = p11.C_CreateObject(session.id(), attributes);

            supportsRawSecretKeyImport = Boolean.TRUE;
        } catch (PKCS11Exception e) {
            supportsRawSecretKeyImport = Boolean.FALSE;
        } finally {
            releaseSession(session);
        }
    }

    return supportsRawSecretKeyImport;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:Token.java

示例11: engineSign

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
@Override
protected byte[] engineSign() throws SignatureException {
    byte[] s = privateKey.getS().toByteArray();
    ECParameterSpec params = privateKey.getParams();
    // DER OID
    byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params);
    int keySize = params.getCurve().getField().getFieldSize();

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    random.nextBytes(seed);

    byte[] sig;
    try {
        sig = signDigest(getDigestValue(), s, encodedParams, seed);
    } catch (GeneralSecurityException e) {
        throw new SignatureException("Could not sign data", e);
    }

    if (p1363Format) {
        return sig;
    } else {
        return encodeSignature(sig);
    }
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:29,代码来源:ECDSASignature.java

示例12: padV15

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
/**
 * PKCS#1 v1.5 padding (blocktype 1 and 2).
 */
private byte[] padV15(byte[] data) throws BadPaddingException {
    byte[] padded = new byte[paddedSize];
    System.arraycopy(data, 0, padded, paddedSize - data.length, data.length);
    int psSize = paddedSize - 3 - data.length;
    int k = 0;
    padded[k++] = 0;
    padded[k++] = (byte)type;
    if (type == PAD_BLOCKTYPE_1) {
        // blocktype 1: all padding bytes are 0xff
        while (psSize-- > 0) {
            padded[k++] = (byte)0xff;
        }
    } else {
        // blocktype 2: padding bytes are random non-zero bytes
        if (random == null) {
            random = JCAUtil.getSecureRandom();
        }
        // generate non-zero padding bytes
        // use a buffer to reduce calls to SecureRandom
        byte[] r = new byte[64];
        int i = -1;
        while (psSize-- > 0) {
            int b;
            do {
                if (i < 0) {
                    random.nextBytes(r);
                    i = r.length - 1;
                }
                b = r[i--] & 0xff;
            } while (b == 0);
            padded[k++] = (byte)b;
        }
    }
    return padded;
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:39,代码来源:RSAPadding.java

示例13: getSigningRandom

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
private SecureRandom getSigningRandom() {
    if (signingRandom == null) {
        if (appRandom != null) {
            signingRandom = appRandom;
        } else {
            signingRandom = JCAUtil.getSecureRandom();
        }
    }
    return signingRandom;
}
 
开发者ID:greghaskins,项目名称:openjdk-jdk7u-jdk,代码行数:11,代码来源:DSA.java

示例14: synchronized

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
/**
 * Return valid RSA blinding parameters for the given private key.
 * Use cached parameters if available. If not, generate new parameters
 * and cache.
 */
private static BlindingParameters getBlindingParameters
        (RSAPrivateCrtKey key) {
    BigInteger modulus = key.getModulus();
    BigInteger e = key.getPublicExponent();
    BlindingParameters params;
    // we release the lock between get() and put()
    // that means threads might concurrently generate new blinding
    // parameters for the same modulus. this is only a slight waste
    // of cycles and seems preferable in terms of scalability
    // to locking out all threads while generating new parameters
    synchronized (blindingCache) {
        params = blindingCache.get(modulus);
    }
    if ((params != null) && params.valid(e)) {
        return params;
    }
    int len = modulus.bitLength();
    SecureRandom random = JCAUtil.getSecureRandom();
    BigInteger r = new BigInteger(len, random).mod(modulus);
    BigInteger re = r.modPow(e, modulus);
    BigInteger rInv = r.modInverse(modulus);
    params = new BlindingParameters(e, re, rInv);
    synchronized (blindingCache) {
        blindingCache.put(modulus, params);
    }
    return params;
}
 
开发者ID:greghaskins,项目名称:openjdk-jdk7u-jdk,代码行数:33,代码来源:RSACore.java

示例15: generateKeyPair

import sun.security.jca.JCAUtil; //导入方法依赖的package包/类
@Override
public KeyPair generateKeyPair() {

    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(null, (ECParameterSpec)params);

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    if (random == null) {
        random = JCAUtil.getSecureRandom();
    }
    random.nextBytes(seed);

    try {

        long[] handles = generateECKeyPair(keySize, encodedParams, seed);

        // The 'params' object supplied above is equivalent to the native
        // one so there is no need to fetch it.

        // handles[0] points to the native private key
        BigInteger s = new BigInteger(1, getEncodedBytes(handles[0]));

        PrivateKey privateKey =
            new ECPrivateKeyImpl(s, (ECParameterSpec)params);

        // handles[1] points to the native public key
        ECPoint w = ECUtil.decodePoint(getEncodedBytes(handles[1]),
            ((ECParameterSpec)params).getCurve());
        PublicKey publicKey =
            new ECPublicKeyImpl(w, (ECParameterSpec)params);

        return new KeyPair(publicKey, privateKey);

    } catch (Exception e) {
        throw new ProviderException(e);
    }
}
 
开发者ID:RedlineResearch,项目名称:OLD-OpenJDK8,代码行数:39,代码来源:ECKeyPairGenerator.java


注:本文中的sun.security.jca.JCAUtil.getSecureRandom方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。