public byte[] toBytes() { ByteArrayOutputStream bs = new ByteArrayOutputStream(); try { bs.write((byte)scheme.ordinal()); if (scheme == SignatureScheme.SM3WITHSM2) { // adding the ID bs.write(((SM2ParameterSpec)param).getID()); // padding a 0 as the terminator bs.write((byte)0); } bs.write(value); } catch (IOException e) { e.printStackTrace(); } return bs.toByteArray(); }
public Signature(byte[] data) throws Exception { if (data == null) { throw new SDKException(ErrorCode.ParamError); } if (data.length < 2) { throw new Exception(ErrorCode.InvalidSignatureDataLen); } this.scheme = SignatureScheme.values()[data[0]]; if (scheme == SignatureScheme.SM3WITHSM2) { int i = 0; while (i < data.length && data[i] != 0){ i++; } if (i >= data.length) { throw new Exception(ErrorCode.InvalidSignatureData); } this.param = new SM2ParameterSpec(Arrays.copyOfRange(data, 1, i)); this.value = Arrays.copyOfRange(data, i + 1, data.length); } else { this.value = Arrays.copyOfRange(data, 1, data.length); } }
public byte[] toBytes() { ByteArrayOutputStream bs = new ByteArrayOutputStream(); try { bs.write((byte)scheme.ordinal()); if (scheme == SignatureScheme.SM3WITHSM2) { // adding the ID bs.write(((SM2ParameterSpec)param).getID()); // padding a 0 as the terminator bs.write((byte)0); } bs.write(value); } catch (IOException e) { e.printStackTrace(); } return bs.toByteArray(); }
public Signature(byte[] data) throws Exception { if (data == null) { throw new SDKException(ErrorCode.ParamError); } if (data.length < 2) { throw new Exception(ErrorCode.InvalidSignatureDataLen); } this.scheme = SignatureScheme.values()[data[0]]; if (scheme == SignatureScheme.SM3WITHSM2) { int i = 0; while (i < data.length && data[i] != 0){ i++; } if (i >= data.length) { throw new Exception(ErrorCode.InvalidSignatureData); } this.param = new SM2ParameterSpec(Arrays.copyOfRange(data, 1, i)); this.value = Arrays.copyOfRange(data, i + 1, data.length); } else { this.value = Arrays.copyOfRange(data, 1, data.length); } }
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException { CipherParameters param = ECUtils.generatePublicKeyParameter(publicKey); if (paramSpec != null) { param = new ParametersWithID(param, paramSpec.getID()); } signer.init(false, param); }
public byte[] generateSignature(byte[] msg, SignatureScheme scheme, Object param) throws Exception { if (msg == null || msg.length == 0) { throw new Exception(ErrorCode.InvalidMessage); } if (this.privateKey == null) { throw new Exception(ErrorCode.WithoutPrivate); } SignatureHandler ctx = new SignatureHandler(keyType, signatureScheme); AlgorithmParameterSpec paramSpec = null; if (signatureScheme == SignatureScheme.SM3WITHSM2) { if (param instanceof String) { paramSpec = new SM2ParameterSpec(Strings.toByteArray((String) param)); } else if (param == null) { paramSpec = new SM2ParameterSpec("1234567812345678".getBytes()); } else { throw new Exception(ErrorCode.InvalidSM2Signature); } } byte[] signature = new Signature( signatureScheme, paramSpec, ctx.generateSignature(privateKey, msg, paramSpec) ).toBytes(); return signature; }
protected void engineInitSign( PrivateKey privateKey) throws InvalidKeyException { CipherParameters param = ECUtil.generatePrivateKeyParameter(privateKey); if (appRandom != null) { param = new ParametersWithRandom(param, appRandom); } if (paramSpec != null) { signer.init(true, new ParametersWithID(param, paramSpec.getID())); } else { signer.init(true, param); } }
public byte[] generateSignature(byte[] msg, SignatureScheme scheme, Object param) throws Exception { if (msg == null || msg.length == 0) { throw new Exception(ErrorCode.InvalidMessage); } if (this.privateKey == null) { throw new Exception(ErrorCode.WithoutPrivate); } SignatureHandler ctx = new SignatureHandler(keyType, signatureScheme); AlgorithmParameterSpec paramSpec = null; if (signatureScheme == SignatureScheme.SM3WITHSM2) { if (param instanceof String) { paramSpec = new SM2ParameterSpec(Strings.toByteArray((String) param)); } else if (param == null) { paramSpec = new SM2ParameterSpec("1234567812345678".getBytes()); } else { throw new Exception(ErrorCode.InvalidSM2Signature); } } byte[] signature = new Signature( signatureScheme, paramSpec, ctx.generateSignature(privateKey, msg, paramSpec) ).toBytes(); return signature; }