Refine search
static BigInteger signedMessageHashToKey( byte[] messageHash, SignatureData signatureData) throws SignatureException { byte[] r = signatureData.getR(); byte[] s = signatureData.getS(); verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes"); verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes"); int header = signatureData.getV() & 0xFF; // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y, // 0x1D = second key with even y, 0x1E = second key with odd y if (header < 27 || header > 34) { throw new SignatureException("Header byte out of range: " + header); } ECDSASignature sig = new ECDSASignature( new BigInteger(1, signatureData.getR()), new BigInteger(1, signatureData.getS())); int recId = header - 27; BigInteger key = recoverFromSignature(recId, sig, messageHash); if (key == null) { throw new SignatureException("Could not recover public key from signature"); } return key; }
private Optional<String> validateRsaSha256(SecurityEnvironment env, InboundClientDefinition clientDefinition) { try { Signature signature = Signature.getInstance("SHA256withRSA"); signature.initVerify(clientDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException("RSA public key configuration is " + "required")) .publicKey() .orElseThrow(() -> new HttpSignatureException( "Public key is required, yet not configured"))); signature.update(getBytesToSign(env, null)); if (!signature.verify(this.signatureBytes)) { return Optional.of("Signature is not valid"); } return Optional.empty(); } catch (NoSuchAlgorithmException e) { LOGGER.log(Level.FINEST, "SHA256withRSA algorithm not found", e); return Optional.of("SHA256withRSA algorithm not found: " + e.getMessage()); } catch (InvalidKeyException e) { LOGGER.log(Level.FINEST, "Invalid RSA key", e); return Optional.of("Invalid RSA key: " + e.getMessage()); } catch (SignatureException e) { LOGGER.log(Level.FINEST, "Signature exception", e); return Optional.of("SignatureException: " + e.getMessage()); } }
public ECKey getKey() { byte[] r = new byte[32]; byte[] s = new byte[32]; byte v = signature[64]; // todo: remove this when cpp conclude what they do here if (v == 1) v = 28; if (v == 0) v = 27; System.arraycopy(signature, 0, r, 0, 32); System.arraycopy(signature, 32, s, 0, 32); ECKey.ECDSASignature signature = ECKey.ECDSASignature.fromComponents(r, s, v); byte[] msgHash = sha3(wire, 97, wire.length - 97); ECKey outKey = null; try { outKey = ECKey.signatureToKey(msgHash, signature); } catch (SignatureException e) { e.printStackTrace(); } return outKey; }
|| n2 > 21 || (length != 0 && (sigBytes[offset + 1] + 2) > length)) { throw new SignatureException("signature bytes have invalid encoding"); throw new SignatureException("bad argument: byte[] is too small"); r = new BigInteger(bytes); s = new BigInteger(bytes); if (r.signum() != 1 || r.compareTo(q) != -1 || s.signum() != 1 || s.compareTo(q) != -1) { return false;
protected byte[] engineSign() throws SignatureException { BigInteger elgp = key.getParams().getP(); BigInteger pm1 = elgp.subtract(BigInteger.ONE); BigInteger elgg = key.getParams().getG(); BigInteger x = ((ElGamalPrivateKey) key).getX(); boolean ok; do { k = new BigInteger(2048, RandomSource.getInstance()); ok = k.compareTo(pm1) == -1; ok = ok && k.compareTo(BigInteger.ONE) == 1; ok = ok && k.gcd(pm1).equals(BigInteger.ONE); rv = SigUtil.sigBytesToASN1(r, s); } catch (IllegalArgumentException iae) { throw new SignatureException("ASN1", iae);
public static boolean verify(Map<String, byte[]> obj, PublicKey key) throws SignatureException, InvalidKeyException { if(!obj.containsKey(signatureString)) throw new SignatureException("No signature supplied"); Signature signature; try { signature = Signature.getInstance("SHA256withRSA"); } catch (NoSuchAlgorithmException e) { return false; //does not happen } byte[] sigString = obj.get(signatureString); byte[] sig = Base64.getDecoder().decode(sigString); obj.remove(signatureString); signature.initVerify(key); signature.update(obj.toString().getBytes(StandardCharsets.UTF_8)); boolean res = signature.verify(sig); obj.put(signatureString, sigString); return res; }
try { KeyFactory keyPair = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyPair.generatePublic(new RSAPublicKeySpec(new BigInteger("" + "26646109746820229206157738327971569752090211692348400628335016561872593810714980851565627258969379628284446" + "95485621727101111383914892371847123298307135674746058587731715965000418491422033377759596510596132771659601" + "71614521772978783834371305059063805184552675999014093903953357902272537363249004733827686143866515001845855" + "26969464300271560446891592452975408396387087701542295019503281458064645269615535802577242268132904180219203" + "8883202203423153080919807757020166001248942233209849142898672366749341514289888739"), new BigInteger("65537"))); Signature rsaSigner = Signature.getInstance("SHA256withRSA"); rsaSigner.initVerify(publicKey); rsaSigner.update(buttonPublicCurve25519Key); rsaSigner.update(mac.toLowerCase().getBytes()); rsaSigner.update(Utils.hexToBytes(buttonUuid)); e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace();
public static String sign(String data, String PEMEncodedPrivateKey, String charset) { PrivateKey privateKey = getPrivateKeyFromPEM(PEMEncodedPrivateKey); if (privateKey == null) { return null; } try { Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(privateKey); signature.update(data.getBytes(charset)); byte[] signBytes = signature.sign(); return Base64.encodeBase64String(signBytes).replaceAll("\n|\r", ""); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
throws InvalidKeyException, SignatureException { if (signerConfig.certificates.isEmpty()) { throw new SignatureException("No certificates configured for signer"); signedData.certificates = encodeCertificates(signerConfig.certificates); } catch (CertificateEncodingException e) { throw new SignatureException("Failed to encode certificates", e); signature.update(signer.signedData); signatureBytes = signature.sign(); } catch (InvalidKeyException e) { throw new InvalidKeyException("Failed sign using " + jcaSignatureAlgorithm, e); } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | SignatureException e) { throw new SignatureException("Failed sign using " + jcaSignatureAlgorithm, e); signature.update(signer.signedData); if (!signature.verify(signatureBytes)) { throw new SignatureException("Signature did not verify"); throw new InvalidKeyException("Failed to verify generated " + jcaSignatureAlgorithm + " signature using public key from certificate", e); } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | SignatureException e) { throw new SignatureException("Failed to verify generated " + jcaSignatureAlgorithm + " signature using public key from certificate", e);
/** * @param sigBytes ASN.1 R,S */ @Override protected boolean engineVerify(byte[] sigBytes) throws SignatureException { BigInteger elgp = key.getParams().getP(); BigInteger pm1 = elgp.subtract(BigInteger.ONE); BigInteger elgg = key.getParams().getG(); BigInteger y = ((ElGamalPublicKey) key).getY(); if (!(y instanceof NativeBigInteger)) y = new NativeBigInteger(y); byte[] data = digest.digest(); try { BigInteger[] rs = SigUtil.aSN1ToBigInteger(sigBytes, 256); BigInteger r = rs[0]; BigInteger s = rs[1]; if (r.signum() != 1 || s.signum() != 1 || r.compareTo(elgp) != -1 || s.compareTo(pm1) != -1) return false; NativeBigInteger h = new NativeBigInteger(1, data); BigInteger modvalr = r.modPow(s, elgp); BigInteger modvaly = y.modPow(r, elgp); BigInteger modmulval = modvalr.multiply(modvaly).mod(elgp); BigInteger v = elgg.modPow(h, elgp); boolean ok = v.compareTo(modmulval) == 0; return ok; } catch (RuntimeException e) { throw new SignatureException("verify", e); } }
@Override public VSignature sign(byte[] purpose, byte[] message) throws VException { String javaSignAlgorithm = CryptoUtil.javaSigningAlgorithm(VANADIUM_HASH_ALGORITHM); message = CryptoUtil.messageDigest(VANADIUM_HASH_ALGORITHM, message, purpose, this.pubKey); // Sign. Note that the signer will first apply another hash on the message, resulting in: // ECDSA.Sign(Hash(Hash(publicKey) +Hash(message) + Hash(purpose))). try { java.security.Signature sig = java.security.Signature.getInstance(javaSignAlgorithm); sig.initSign(this.privKey); sig.update(message); byte[] asn1Sig = sig.sign(); return CryptoUtil.vanadiumSignature(VANADIUM_HASH_ALGORITHM, purpose, asn1Sig); } catch (NoSuchAlgorithmException e) { throw new VException("Signing algorithm " + javaSignAlgorithm + " not supported by the runtime: " + e.getMessage()); } catch (InvalidKeyException e) { throw new VException("Invalid private key: " + e.getMessage()); } catch (SignatureException e) { throw new VException( "Invalid signing data [ " + Arrays.toString(message) + " ]: " + e.getMessage()); } }
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(privateKey); signature.update(src.getBytes()); byte[] result = signature.sign(); System.out.println("jdk rsa sign : " + Hex.encodeHexString(result)); e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace();
Signature sig = Signature.getInstance(curve.defaultHashAlgorithm.replace("ECDSA", "ECDDSA"), JceLoader.BouncyCastle); sig.initSign(key.getPrivate()); for(byte[] d: data) sig.update(d); result = sig.sign(); e.printStackTrace(); } catch (InvalidKeyException e) { Logger.error(this, "InvalidKeyException : "+e.getMessage(),e); e.printStackTrace(); } catch (SignatureException e) { Logger.error(this, "SignatureException : "+e.getMessage(),e); e.printStackTrace();
@Override public void write(int b) throws IOException { try { signature.update((byte) b); } catch (SignatureException e) { throw new IOException(e.getLocalizedMessage()); } }
public static boolean verify(Curves curve, ECPublicKey key, byte[] signature, int sigoffset, int siglen, byte[]... data) { if(key == null || curve == null || signature == null || data == null) return false; boolean result = false; try { Signature sig = Signature.getInstance(curve.defaultHashAlgorithm, curve.sigProvider); sig.initVerify(key); for(byte[] d: data) sig.update(d); // Strip padding: BC 1.54 cannot deal with it. siglen = actualSignatureLength(signature, sigoffset, siglen); result = sig.verify(signature, sigoffset, siglen); } catch (NoSuchAlgorithmException e) { Logger.error(ECDSA.class, "NoSuchAlgorithmException : "+e.getMessage(),e); e.printStackTrace(); } catch (InvalidKeyException e) { Logger.error(ECDSA.class, "InvalidKeyException : "+e.getMessage(),e); e.printStackTrace(); } catch (SignatureException e) { Logger.error(ECDSA.class, "SignatureException : "+e.getMessage(),e); e.printStackTrace(); } return result; }
v3CertGen.setSerialNumber(BigInteger.valueOf(serial < 0 ? -1 * serial : serial)); v3CertGen.setIssuerDN(principal); v3CertGen.setNotBefore(new Date(System.currentTimeMillis())); e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace();
@Override public void write(int singleByte) throws IOException { try { signer.update((byte) singleByte); } catch (SignatureException ex) { throw new IOException(ex.getMessage(), ex); } }
protected byte[] engineSign() throws SignatureException { byte[] hash = new byte[digest.getDigestSize()]; digest.doFinal(hash, 0); try { BigInteger[] sig = signer.generateSignature(hash); byte[] r = sig[0].toByteArray(); byte[] s = sig[1].toByteArray(); byte[] sigBytes = new byte[(r.length > s.length ? r.length * 2 : s.length * 2)]; System.arraycopy(s, 0, sigBytes, (sigBytes.length / 2) - s.length, s.length); System.arraycopy(r, 0, sigBytes, sigBytes.length - r.length, r.length); return new DEROctetString(sigBytes).getEncoded(); } catch (Exception e) { throw new SignatureException(e.toString()); } }
if (v == null) throw new SignatureException(Messages.MESSAGES.unableToFindHeader(name, (index > 0 ? "[" + index + "]" : ""))); if (i < 0) throw new SignatureException(Messages.MESSAGES.unableToFindHeader(name, (index > 0 ? "[" + index + "]" : ""))); throw new SignatureException(Messages.MESSAGES.unableToFindHeader(name, (index > 0 ? "[" + index + "]" : ""))); signature.update(entry.getBytes()); verifiedHeaders.add(name, v.toString());
@Override public byte[] sign(byte[] data) { try { return doSign(data); } catch (InvalidKeyException e) { throw new SignatureException("Invalid RSA PrivateKey. " + e.getMessage(), e); } catch (java.security.SignatureException e) { throw new SignatureException("Unable to calculate signature using RSA PrivateKey. " + e.getMessage(), e); } }