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


Java KeyUtil.trimZeroes方法代码示例

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


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

示例1: asn1ToECDSA

import sun.security.util.KeyUtil; //导入方法依赖的package包/类
private byte[] asn1ToECDSA(byte[] signature) throws SignatureException {
    try {
        DerInputStream in = new DerInputStream(signature);
        DerValue[] values = in.getSequence(2);
        BigInteger r = values[0].getPositiveBigInteger();
        BigInteger s = values[1].getPositiveBigInteger();
        // trim leading zeroes
        byte[] br = KeyUtil.trimZeroes(r.toByteArray());
        byte[] bs = KeyUtil.trimZeroes(s.toByteArray());
        int k = Math.max(br.length, bs.length);
        // r and s each occupy half the array
        byte[] res = new byte[k << 1];
        System.arraycopy(br, 0, res, k - br.length, br.length);
        System.arraycopy(bs, 0, res, res.length - bs.length, bs.length);
        return res;
    } catch (Exception e) {
        throw new SignatureException("invalid encoding for signature", e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:20,代码来源:P11Signature.java

示例2: asn1ToECDSA

import sun.security.util.KeyUtil; //导入方法依赖的package包/类
private byte[] asn1ToECDSA(byte[] sig) throws SignatureException {
    try {
        // Enforce strict DER checking for signatures
        DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
        DerValue[] values = in.getSequence(2);

        // check number of components in the read sequence
        // and trailing data
        if ((values.length != 2) || (in.available() != 0)) {
            throw new IOException("Invalid encoding for signature");
        }

        BigInteger r = values[0].getPositiveBigInteger();
        BigInteger s = values[1].getPositiveBigInteger();

        // trim leading zeroes
        byte[] br = KeyUtil.trimZeroes(r.toByteArray());
        byte[] bs = KeyUtil.trimZeroes(s.toByteArray());
        int k = Math.max(br.length, bs.length);
        // r and s each occupy half the array
        byte[] res = new byte[k << 1];
        System.arraycopy(br, 0, res, k - br.length, br.length);
        System.arraycopy(bs, 0, res, res.length - bs.length, bs.length);
        return res;
    } catch (Exception e) {
        throw new SignatureException("Invalid encoding for signature", e);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:P11Signature.java

示例3: nativeGenerateSecret

import sun.security.util.KeyUtil; //导入方法依赖的package包/类
private SecretKey nativeGenerateSecret(String algorithm)
        throws IllegalStateException, NoSuchAlgorithmException,
        InvalidKeyException {
    if ((privateKey == null) || (publicValue == null)) {
        throw new IllegalStateException("Not initialized correctly");
    }
    long keyType = CKK_GENERIC_SECRET;
    Session session = null;
    try {
        session = token.getObjSession();
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
            new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
            new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
        };
        attributes = token.getAttributes
            (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
        long keyID = token.p11.C_DeriveKey(session.id(),
            new CK_MECHANISM(mechanism, publicValue), privateKey.keyID,
            attributes);
        CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {
            new CK_ATTRIBUTE(CKA_VALUE_LEN),
        };
        token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);
        int keyLen = (int)lenAttributes[0].getLong();
        SecretKey key = P11Key.secretKey
                    (session, keyID, algorithm, keyLen << 3, attributes);
        if ("RAW".equals(key.getFormat())) {
            // Workaround for Solaris bug 6318543.
            // Strip leading zeroes ourselves if possible (key not sensitive).
            // This should be removed once the Solaris fix is available
            // as here we always retrieve the CKA_VALUE even for tokens
            // that do not have that bug.
            byte[] keyBytes = key.getEncoded();
            byte[] newBytes = KeyUtil.trimZeroes(keyBytes);
            if (keyBytes != newBytes) {
                key = new SecretKeySpec(newBytes, algorithm);
            }
        }
        return key;
    } catch (PKCS11Exception e) {
        throw new InvalidKeyException("Could not derive key", e);
    } finally {
        publicValue = null;
        token.releaseSession(session);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:47,代码来源:P11KeyAgreement.java


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