/** * SM3计算. * * @param datas * 待计算的数据 * @return 计算结果 */ private static byte[] sm3(byte[] data) { SM3Digest sm3 = new SM3Digest(); sm3.update(data, 0, data.length); byte[] result = new byte[sm3.getDigestSize()]; sm3.doFinal(result, 0); return result; }
int wj13 = this.W[j - 13]; int r7 = ((wj13 << 7) | (wj13 >>> (32 - 7))); this.W[j] = P1(this.W[j - 16] ^ this.W[j - 9] ^ r15) ^ r7 ^ this.W[j - 6]; int SS1 = ((s1_ << 7) | (s1_ >>> (32 - 7))); int SS2 = SS1 ^ a12; int TT1 = FF0(A, B, C) + D + SS2 + this.W1[j]; int TT2 = GG0(E, F, G) + H + SS1 + this.W[j]; D = C; C = ((B << 9) | (B >>> (32 - 9))); G = ((F << 19) | (F >>> (32 - 19))); F = E; E = P0(TT2); int SS1 = ((s1_ << 7) | (s1_ >>> (32 - 7))); int SS2 = SS1 ^ a12; int TT1 = FF1(A, B, C) + D + SS2 + this.W1[j]; int TT2 = GG1(E, F, G) + H + SS1 + this.W[j]; D = C; C = ((B << 9) | (B >>> (32 - 9))); G = ((F << 19) | (F >>> (32 - 19))); F = E; E = P0(TT2);
public SM2Engine() { this(new SM3Digest()); }
private byte[] getZ(byte[] userID) { digest.reset(); addUserID(digest, userID); addFieldElement(digest, ecParams.getCurve().getA()); addFieldElement(digest, ecParams.getCurve().getB()); addFieldElement(digest, ecParams.getG().getAffineXCoord()); addFieldElement(digest, ecParams.getG().getAffineYCoord()); addFieldElement(digest, pubPoint.getAffineXCoord()); addFieldElement(digest, pubPoint.getAffineYCoord()); byte[] result = new byte[digest.getDigestSize()]; digest.doFinal(result, 0); return result; }
private byte[] digestDoFinal() { byte[] result = new byte[digest.getDigestSize()]; digest.doFinal(result, 0); reset(); return result; }
public void update(byte b) { digest.update(b); }
int wj13 = this.W[j - 13]; int r7 = ((wj13 << 7) | (wj13 >>> (32 - 7))); this.W[j] = P1(this.W[j - 16] ^ this.W[j - 9] ^ r15) ^ r7 ^ this.W[j - 6]; int Wj = W[j]; int W1j = Wj ^ W[j + 4]; int TT1 = FF0(A, B, C) + D + SS2 + W1j; int TT2 = GG0(E, F, G) + H + SS1 + Wj; D = C; C = ((B << 9) | (B >>> (32 - 9))); G = ((F << 19) | (F >>> (32 - 19))); F = E; E = P0(TT2); int Wj = W[j]; int W1j = Wj ^ W[j + 4]; int TT1 = FF1(A, B, C) + D + SS2 + W1j; int TT2 = GG1(E, F, G) + H + SS1 + Wj; D = C; C = ((B << 9) | (B >>> (32 - 9))); G = ((F << 19) | (F >>> (32 - 19))); F = E; E = P0(TT2);
public SM2KeyExchange() { this(new SM3Digest()); }
public void update(byte[] in, int off, int len) { digest.update(in, off, len); }
public static byte[] hash(byte[] srcData) { SM3Digest digest = new SM3Digest(); digest.update(srcData, 0, srcData.length); byte[] hash = new byte[digest.getDigestSize()]; digest.doFinal(hash, 0); return hash; }
public Digest() { super(new SM3Digest()); }
digest.update(z, 0, z.length);
/** * SM3计算. * * @param datas * 待计算的数据 * @return 计算结果 */ private static byte[] sm3(byte[] data) { SM3Digest sm3 = new SM3Digest(); sm3.update(data, 0, data.length); byte[] result = new byte[sm3.getDigestSize()]; sm3.doFinal(result, 0); return result; }
public Memoable copy() { return new SM3Digest(this); }
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; }
public Memoable copy() { return new SM3Digest(this); }
public Object clone() throws CloneNotSupportedException { Digest d = (Digest)super.clone(); d.digest = new SM3Digest((SM3Digest)digest); return d; } }
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; } }
public Digest createDigest() { switch (this) { case SHA1: return new SHA1Digest(); case SHA224: return new SHA224Digest(); case SHA256: return new SHA256Digest(); case SHA384: return new SHA384Digest(); case SHA512: return new SHA512Digest(); case SHA3_224: return new SHA3Digest(224); case SHA3_256: return new SHA3Digest(256); case SHA3_384: return new SHA3Digest(384); case SHA3_512: return new SHA3Digest(512); case SM3: return new SM3Digest(); default: throw new IllegalStateException("should not reach here, unknown HashAlgo " + name()); } }