org.bouncycastle.crypto.digests.SM3Digest Java Examples

The following examples show how to use org.bouncycastle.crypto.digests.SM3Digest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HmacSignUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
public static Mac getHmac(Digest digest, byte[] key) {
    KeyParameter keyParameter = new KeyParameter(key);
    HMac hmac = new HMac(new SM3Digest());
    hmac.init(keyParameter);

    return new Mac() {
        @Override
        public byte[] doFinal(byte[] bytes) {
            hmac.update(bytes, 0, bytes.length);
            byte[] result = new byte[hmac.getMacSize()];
            hmac.doFinal(result, 0);
            return result;
        }
    };

}
 
Example #2
Source File: HmacSignUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
public static Mac getHmac(HMacAlgorithms macAlgorithms, byte[] key) {
    try {
        switch (macAlgorithms) {
            case HMAC_SM3:
                return getHmac(new SM3Digest(), key);
            default:
                javax.crypto.Mac mac = javax.crypto.Mac.getInstance(macAlgorithms.getName());
                mac.init(new SecretKeySpec(key, macAlgorithms.getName()));
                return new Mac() {
                    @Override
                    public byte[] doFinal(byte[] bytes) {
                        return mac.doFinal(bytes);
                    }
                };
        }

    } catch (Exception e) {

        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: GMUtil.java    From xipki with Apache License 2.0 6 votes vote down vote up
public static byte[] getSM2Z(byte[] userID, ASN1ObjectIdentifier curveOid,
    BigInteger pubPointX, BigInteger pubPointY) {
  SM3Digest digest = new SM3Digest();

  addUserId(digest, userID);

  X9ECParameters ecParams = GMNamedCurves.getByOID(curveOid);
  addFieldElement(digest, ecParams.getCurve().getA());
  addFieldElement(digest, ecParams.getCurve().getB());
  addFieldElement(digest, ecParams.getG().getAffineXCoord());
  addFieldElement(digest, ecParams.getG().getAffineYCoord());

  int fieldSize = (ecParams.getCurve().getFieldSize() + 7) / 8;
  byte[] bytes = BigIntegers.asUnsignedByteArray(fieldSize, pubPointX);
  digest.update(bytes, 0, fieldSize);

  bytes = BigIntegers.asUnsignedByteArray(fieldSize, pubPointY);
  digest.update(bytes, 0, fieldSize);

  byte[] result = new byte[digest.getDigestSize()];
  digest.doFinal(result, 0);
  return result;
}
 
Example #4
Source File: SM3Util.java    From javasdk with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * sm3 hash.
 * @param sourceData source data
 * @return digest of sm3
 */
public static byte[] hash(byte[] sourceData) {
    SM3Digest sm3Digest = new SM3Digest();
    sm3Digest.update(sourceData, 0, sourceData.length);
    byte[] md = new byte[sm3Digest.getDigestSize()];
    sm3Digest.doFinal(md, 0);
    return md;
}
 
Example #5
Source File: SM3Util.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * 计算SM3 Mac值
 *
 * @param key     key值,可以是任意长度的字节数组
 * @param srcData 原文
 * @return Mac值,对于HMac-SM3来说是32字节
 */
public static byte[] hmac(byte[] key, byte[] srcData) {
    KeyParameter keyParameter = new KeyParameter(key);
    SM3Digest digest = new SM3Digest();
    HMac mac = new HMac(digest);
    mac.init(keyParameter);
    mac.update(srcData, 0, srcData.length);
    byte[] result = new byte[mac.getMacSize()];
    mac.doFinal(result, 0);
    return result;
}
 
Example #6
Source File: Sm3Util.java    From littleca with Apache License 2.0 5 votes vote down vote up
public static byte[] hmac(byte[] key, byte[] srcData) {
    KeyParameter keyParameter = new KeyParameter(key);
    SM3Digest digest = new SM3Digest();
    HMac mac = new HMac(digest);
    mac.init(keyParameter);
    mac.update(srcData, 0, srcData.length);
    byte[] result = new byte[mac.getMacSize()];
    mac.doFinal(result, 0);
    return result;
}
 
Example #7
Source File: BouncyCastleHasher.java    From hash-bench with MIT License 4 votes vote down vote up
public static final void register(final Map<String, Hasher> hashers) {
  hashers.put(BouncyCastleHasher.GOST,
          new BouncyCastleHasher(new GOST3411Digest()));
  hashers.put(BouncyCastleHasher.MD2,
          new BouncyCastleHasher(new MD2Digest()));
  hashers.put(BouncyCastleHasher.MD4,
          new BouncyCastleHasher(new MD4Digest()));
  hashers.put(BouncyCastleHasher.MD5,
          new BouncyCastleHasher(new MD5Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD128,
          new BouncyCastleHasher(new RIPEMD128Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD160,
          new BouncyCastleHasher(new RIPEMD160Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD256,
          new BouncyCastleHasher(new RIPEMD256Digest()));
  hashers.put(BouncyCastleHasher.RIPEMD320,
          new BouncyCastleHasher(new RIPEMD320Digest()));
  hashers.put(BouncyCastleHasher.SHA1,
          new BouncyCastleHasher(new SHA1Digest()));
  hashers.put(BouncyCastleHasher.SHA224,
          new BouncyCastleHasher(new SHA224Digest()));
  hashers.put(BouncyCastleHasher.SHA256,
          new BouncyCastleHasher(new SHA256Digest()));
  hashers.put(BouncyCastleHasher.SHA3,
          new BouncyCastleHasher(new SHA3Digest()));
  hashers.put(BouncyCastleHasher.SHA384,
          new BouncyCastleHasher(new SHA384Digest()));
  hashers.put(BouncyCastleHasher.SHA512,
          new BouncyCastleHasher(new SHA512Digest()));
  hashers.put(BouncyCastleHasher.SHA512_T,
          new BouncyCastleHasher(new SHA512tDigest(7 * 8)));
  hashers.put(BouncyCastleHasher.SKEIN1024, new BouncyCastleHasher(
          new SkeinDigest(SkeinDigest.SKEIN_1024, Long.BYTES * 8)));
  hashers.put(BouncyCastleHasher.SKEIN256, new BouncyCastleHasher(
          new SkeinDigest(SkeinDigest.SKEIN_256, Long.BYTES * 8)));
  hashers.put(BouncyCastleHasher.SKEIN512, new BouncyCastleHasher(
          new SkeinDigest(SkeinDigest.SKEIN_512, Long.BYTES * 8)));
  hashers.put(BouncyCastleHasher.SM3,
          new BouncyCastleHasher(new SM3Digest()));
  hashers.put(BouncyCastleHasher.TIGER,
          new BouncyCastleHasher(new TigerDigest()));
  hashers.put(BouncyCastleHasher.WHIRLPOOL2,
          new BouncyCastleHasher(new WhirlpoolDigest()));
}
 
Example #8
Source File: SM2PreprocessSigner.java    From gmhelper with Apache License 2.0 2 votes vote down vote up
/**
 * 初始化
 *
 * @param forSigning true表示用于签名,false表示用于验签
 * @param param
 */
public void init(boolean forSigning, CipherParameters param) {
    init(forSigning, new SM3Digest(), param);
}