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; }
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; }
/** * 可以看到交易数据本身是没有加密的,是可以直接获取到。 * v r s是用私钥加密的数据,利用v r s加上交易数据可以得到私钥对应的公钥及地址。 * 所以RawTransaction里是没有fromAddress的参数的。 * 解密出的地址就是发出交易的地址。这样一来完成了验证。 */ private static void decodeMessageV340(String signedData) { System.out.println("解密 start " + System.currentTimeMillis()); RawTransaction rawTransaction = TransactionDecoder.decode(signedData); if (rawTransaction instanceof SignedRawTransaction) { try { String from = ((SignedRawTransaction) rawTransaction).getFrom(); System.out.println("address " + from); } catch (SignatureException e) { e.printStackTrace(); } } System.out.println("解密 end " + System.currentTimeMillis()); } }
/** * * google openrtb-doubleclick winnotice price decrypt * @param priceCipher 待解密的价格 * @param ikey ikey 完整性校验key * @param ekey ikey 价格加密key * @param isUrlSafe 价格是否使用base64UrlSafe进行编码 * @return */ public static long decryptPrice(String priceCipher, String ikey, String ekey, boolean isUrlSafe) { long price = -1l; try { byte[] unbase64Price = decodePrice(priceCipher, isUrlSafe); if (unbase64Price.length != (OVERHEAD_SIZE + PAYLOAD_SIZE)) { throw new IllegalArgumentException(String.format( "Price is %s bytes, should be %s", unbase64Price.length, (OVERHEAD_SIZE + PAYLOAD_SIZE))); } byte[] plainData = decrypt(unbase64Price, ikey, ekey); price = ByteBuffer.wrap(plainData).getLong(PAYLOAD_BASE); } catch (SignatureException e) { e.printStackTrace(); } return price; }
public static byte[] sign(byte[] data, KeyPair pair) { Signature signer = null; try { signer = Signature.getInstance(CertManagerConstants.CERT_ALGORITHM.SHA1withRSA.toString(), CertManagerConstants.BC); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (NoSuchProviderException e) { e.printStackTrace(); return null; } try { signer.initSign(pair.getPrivate()); } catch (InvalidKeyException e) { e.printStackTrace(); return null; } try { signer.update(data); } catch (SignatureException e) { e.printStackTrace(); return null; } try { return signer.sign(); } catch (SignatureException e) { e.printStackTrace(); return null; } }
signer.update(data); } catch (SignatureException e) { e.printStackTrace(); return null; return signer.sign(); } catch (SignatureException e) { e.printStackTrace(); return null;
e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace();
} catch (SignatureException e) { Logger.error(this, "SignatureException : "+e.getMessage(),e); e.printStackTrace();
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; }
e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace();
e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace();
e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace();
e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace();
@Test public void testValidateWebhook() { Map<String, String> headers = new HashMap<String, String>(); APIContext apiContext = new APIContext(); Map<String, String> configs = new HashMap<String, String>(); configs.put(Constants.PAYPAL_WEBHOOK_ID, "3RN13029J36659323"); apiContext.setConfigurationMap(configs); headers.put(Constants.PAYPAL_HEADER_CERT_URL, "https://api.sandbox.paypal.com/v1/notifications/certs/CERT-360caa42-fca2a594-a5cafa77"); headers.put(Constants.PAYPAL_HEADER_TRANSMISSION_ID, "b2384410-f8d2-11e4-8bf3-77339302725b"); headers.put(Constants.PAYPAL_HEADER_TRANSMISSION_TIME, "2015-05-12T18:14:14Z"); headers.put(Constants.PAYPAL_HEADER_AUTH_ALGO, "SHA256withRSA"); headers.put(Constants.PAYPAL_HEADER_TRANSMISSION_SIG, "vSOIQFIZQHv8G2vpbOpD/4fSC4/MYhdHyv+AmgJyeJQq6q5avWyHIe/zL6qO5hle192HSqKbYveLoFXGJun2od2zXN3Q45VBXwdX3woXYGaNq532flAtiYin+tQ/0pNwRDsVIufCxa3a8HskaXy+YEfXNnwCSL287esD3HgOHmuAs0mYKQdbR4e8Evk8XOOQaZzGeV7GNXXz19gzzvyHbsbHmDz5VoRl9so5OoHqvnc5RtgjZfG8KA9lXh2MTPSbtdTLQb9ikKYnOGM+FasFMxk5stJisgmxaefpO9Q1qm3rCjaJ29aAOyDNr3Q7WkeN3w4bSXtFMwyRBOF28pJg9g=="); String requestBody = "{\"id\":\"WH-2W7266712B616591M-36507203HX6402335\",\"create_time\":\"2015-05-12T18:14:14Z\",\"resource_type\":\"sale\",\"event_type\":\"PAYMENT.SALE.COMPLETED\",\"summary\":\"Payment completed for $ 20.0 USD\",\"resource\":{\"id\":\"7DW85331GX749735N\",\"create_time\":\"2015-05-12T18:13:18Z\",\"update_time\":\"2015-05-12T18:13:36Z\",\"amount\":{\"total\":\"20.00\",\"currency\":\"USD\"},\"payment_mode\":\"INSTANT_TRANSFER\",\"state\":\"completed\",\"protection_eligibility\":\"ELIGIBLE\",\"protection_eligibility_type\":\"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE\",\"parent_payment\":\"PAY-1A142943SV880364LKVJEFPQ\",\"transaction_fee\":{\"value\":\"0.88\",\"currency\":\"USD\"},\"links\":[{\"href\":\"https://api.sandbox.paypal.com/v1/payments/sale/7DW85331GX749735N\",\"rel\":\"self\",\"method\":\"GET\"},{\"href\":\"https://api.sandbox.paypal.com/v1/payments/sale/7DW85331GX749735N/refund\",\"rel\":\"refund\",\"method\":\"POST\"},{\"href\":\"https://api.sandbox.paypal.com/v1/payments/payment/PAY-1A142943SV880364LKVJEFPQ\",\"rel\":\"parent_payment\",\"method\":\"GET\"}]},\"links\":[{\"href\":\"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-2W7266712B616591M-36507203HX6402335\",\"rel\":\"self\",\"method\":\"GET\"},{\"href\":\"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-2W7266712B616591M-36507203HX6402335/resend\",\"rel\":\"resend\",\"method\":\"POST\"}]}"; try { Event.validateReceivedEvent(apiContext, headers, requestBody); } catch (PayPalRESTException e) { e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SignatureException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
} catch (SignatureException e) { setStatus("Error creating signature. (" + e.getMessage() + ")"); e.printStackTrace(); } catch (InvalidKeySpecException e) { setStatus("Unsupported key specifications. (" + e.getMessage() + ")");
public static boolean validTransaction(Transaction signedTransaction) { assert (signedTransaction.getSignatureCount() == signedTransaction.getRawData().getContractCount()); List<Transaction.Contract> listContract = signedTransaction.getRawData().getContractList(); byte[] hash = Sha256Hash.hash(signedTransaction.getRawData().toByteArray()); int count = signedTransaction.getSignatureCount(); if (count == 0) { return false; } for (int i = 0; i < count; ++i) { try { Transaction.Contract contract = listContract.get(i); byte[] owner = getOwner(contract); byte[] address = ECKey .signatureToAddress(hash, getBase64FromByteString(signedTransaction.getSignature(i))); if (!Arrays.equals(owner, address)) { return false; } } catch (SignatureException e) { e.printStackTrace(); return false; } } return true; }
} catch (SignatureException e) { setStatus("Error creating signature. (" + e.getMessage() + ")"); e.printStackTrace(); } catch (InvalidKeySpecException e) { setStatus("Unsupported key specifications. (" + e.getMessage() + ")");