org.bouncycastle.crypto.params.ECPrivateKeyParameters Java Examples
The following examples show how to use
org.bouncycastle.crypto.params.ECPrivateKeyParameters.
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: SM2UtilTest.java From gmhelper with Apache License 2.0 | 6 votes |
@Test public void testSM2KeyRecovery() { try { String priHex = "5DD701828C424B84C5D56770ECF7C4FE882E654CAC53C7CC89A66B1709068B9D"; String xHex = "FF6712D3A7FC0D1B9E01FF471A87EA87525E47C7775039D19304E554DEFE0913"; String yHex = "F632025F692776D4C13470ECA36AC85D560E794E1BCCF53D82C015988E0EB956"; String encodedPubHex = "04FF6712D3A7FC0D1B9E01FF471A87EA87525E47C7775039D19304E554DEFE0913F632025F692776D4C13470ECA36AC85D560E794E1BCCF53D82C015988E0EB956"; String signHex = "30450220213C6CD6EBD6A4D5C2D0AB38E29D441836D1457A8118D34864C247D727831962022100D9248480342AC8513CCDF0F89A2250DC8F6EB4F2471E144E9A812E0AF497F801"; byte[] signBytes = ByteUtils.fromHexString(signHex); byte[] src = ByteUtils.fromHexString("0102030405060708010203040506070801020304050607080102030405060708"); byte[] withId = ByteUtils.fromHexString("31323334353637383132333435363738"); ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( new BigInteger(ByteUtils.fromHexString(priHex)), SM2Util.DOMAIN_PARAMS); ECPublicKeyParameters pubKey = BCECUtil.createECPublicKeyParameters(xHex, yHex, SM2Util.CURVE, SM2Util.DOMAIN_PARAMS); if (!SM2Util.verify(pubKey, src, signBytes)) { Assert.fail("verify failed"); } } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } }
Example #2
Source File: LocalIdentity.java From ts3j with Apache License 2.0 | 6 votes |
/** * Generates a new identity with a given security level target. * @param securityLevel security level to generate for (may take time) * @return local identity with given security level * @throws GeneralSecurityException */ public static LocalIdentity generateNew(int securityLevel) throws GeneralSecurityException { ECNamedCurveParameterSpec ecp = ECNamedCurveTable.getParameterSpec("prime256v1"); ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom()); ECKeyPairGenerator generator = new ECKeyPairGenerator(); generator.init(keyGenParams); AsymmetricCipherKeyPair keyPair = generator.generateKeyPair(); ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic(); LocalIdentity localIdentity = load(publicKey.getQ().normalize(), privateKey.getD()); localIdentity.improveSecurity(securityLevel); return localIdentity; }
Example #3
Source File: ECDSASigner.java From web3sdk with Apache License 2.0 | 6 votes |
@Override public void init(boolean forSigning, CipherParameters param) { SecureRandom providedRandom = null; if (forSigning) { if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom) param; this.key = (ECPrivateKeyParameters) rParam.getParameters(); providedRandom = rParam.getRandom(); } else { this.key = (ECPrivateKeyParameters) param; } } else { this.key = (ECPublicKeyParameters) param; } this.random = initSecureRandom(forSigning && !kCalculator.isDeterministic(), providedRandom); }
Example #4
Source File: AccountServiceImpl.java From javasdk with GNU Lesser General Public License v3.0 | 6 votes |
@Override public Account genAccount(Algo algo, String password) { byte[] address; byte[] publicKey; byte[] privateKey; ECKey ecKey; AsymmetricCipherKeyPair keyPair; if (algo.isSM()) { keyPair = SM2Util.generateKeyPair(); ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters ecPub = (ECPublicKeyParameters) keyPair.getPublic(); BigInteger privateKeyBI = ecPriv.getD(); publicKey = ecPub.getQ().getEncoded(false); privateKey = Account.encodePrivateKey(ByteUtil.biConvert32Bytes(privateKeyBI), algo, password); address = HashUtil.sha3omit12(publicKey); return new SMAccount(ByteUtil.toHex(address), ByteUtil.toHex(publicKey), ByteUtil.toHex(privateKey), Version.V4, algo, keyPair); } else { ecKey = new ECKey(new SecureRandom()); address = ecKey.getAddress(); publicKey = ecKey.getPubKey(); privateKey = Account.encodePrivateKey(ecKey.getPrivKeyBytes(), algo, password); return new ECAccount(ByteUtil.toHex(address), ByteUtil.toHex(publicKey), ByteUtil.toHex(privateKey), Version.V4, algo, ecKey); } }
Example #5
Source File: BouncyCastleCrypto.java From fabric-api with Apache License 2.0 | 6 votes |
@Override public byte[] sign(byte[] hash, byte[] privateKey) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(new BigInteger(privateKey), domain)); BigInteger[] signature = signer.generateSignature(hash); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(toCanonicalS(signature[1]))); seq.close(); return baos.toByteArray(); } catch (IOException e) { return new byte[0]; } }
Example #6
Source File: ECKeyPair.java From bop-bitcoin-client with Apache License 2.0 | 6 votes |
@Override public byte[] sign (byte[] hash) throws ValidationException { if ( priv == null ) { throw new ValidationException ("Need private key to sign"); } ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ())); signer.init (true, new ECPrivateKeyParameters (priv, domain)); BigInteger[] signature = signer.generateSignature (hash); ByteArrayOutputStream s = new ByteArrayOutputStream (); try { DERSequenceGenerator seq = new DERSequenceGenerator (s); seq.addObject (new ASN1Integer (signature[0])); seq.addObject (new ASN1Integer (signature[1])); seq.close (); return s.toByteArray (); } catch ( IOException e ) { } return null; }
Example #7
Source File: Signature.java From evt4j with MIT License | 6 votes |
public static Signature signHash(byte[] hash, @NotNull PrivateKey key) { checkHashLength(hash); // init deterministic k calculator Signer signer = new Signer(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(key.getD(), ECKey.CURVE); signer.init(true, privateKeyParameters); BigInteger[] components = signer.generateSignature(hash); Signature sig = new Signature(components[0], components[1]).toCanonicalised(); // find the recId and store in signature for public key recover later PublicKey publicKey = key.toPublicKey(); int recId = getRecId(sig, hash, publicKey); if (recId == -1) { throw new RecoverIDNotFoundException(); } sig.setRecId(recId); return sig; }
Example #8
Source File: SM2KeyExchangeUtil.java From gmhelper with Apache License 2.0 | 6 votes |
/** * @param initiator true表示发起方,false表示响应方 * @param keyBits 生成的密钥长度 * @param confirmationTag 确认信息,如果是响应方可以为null;如果是发起方则应为响应方的s1 * @param selfStaticPriv 己方固定私钥 * @param selfEphemeralPriv 己方临时私钥 * @param selfId 己方ID * @param otherStaticPub 对方固定公钥 * @param otherEphemeralPub 对方临时公钥 * @param otherId 对方ID * @return */ public static ExchangeResult calculateKeyWithConfirmation(boolean initiator, int keyBits, byte[] confirmationTag, ECPrivateKeyParameters selfStaticPriv, ECPrivateKeyParameters selfEphemeralPriv, byte[] selfId, ECPublicKeyParameters otherStaticPub, ECPublicKeyParameters otherEphemeralPub, byte[] otherId) { SM2KeyExchange exch = new SM2KeyExchange(); exch.init(new ParametersWithID( new SM2KeyExchangePrivateParameters(initiator, selfStaticPriv, selfEphemeralPriv), selfId)); byte[][] result = exch.calculateKeyWithConfirmation( keyBits, confirmationTag, new ParametersWithID(new SM2KeyExchangePublicParameters(otherStaticPub, otherEphemeralPub), otherId)); ExchangeResult confirmResult = new ExchangeResult(); confirmResult.setKey(result[0]); if (initiator) { confirmResult.setS2(result[1]); } else { confirmResult.setS1(result[1]); confirmResult.setS2(result[2]); } return confirmResult; }
Example #9
Source File: TransactionUtil.java From chain33-sdk-java with BSD 2-Clause "Simplified" License | 6 votes |
/** * @description 创建私钥和公钥 * * @return 私钥 */ public static byte[] generatorPrivateKey() { int length = 0; byte[] privateKey; do { ECKeyPairGenerator gen = new ECKeyPairGenerator(); SecureRandom secureRandom = new SecureRandom(); X9ECParameters secnamecurves = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(), secnamecurves.getN(), secnamecurves.getH()); ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom); gen.init(keyGenParam); AsymmetricCipherKeyPair kp = gen.generateKeyPair(); ECPrivateKeyParameters privatekey = (ECPrivateKeyParameters) kp.getPrivate(); privateKey = privatekey.getD().toByteArray(); length = privatekey.getD().toByteArray().length; } while (length != 32); return privateKey; }
Example #10
Source File: ECKeyPair.java From WalletCordova with GNU Lesser General Public License v2.1 | 6 votes |
@Override public byte[] sign (byte[] hash) throws ValidationException { if ( priv == null ) { throw new ValidationException ("Need private key to sign"); } ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ())); signer.init (true, new ECPrivateKeyParameters (priv, domain)); BigInteger[] signature = signer.generateSignature (hash); ByteArrayOutputStream s = new ByteArrayOutputStream (); try { DERSequenceGenerator seq = new DERSequenceGenerator (s); seq.addObject (new ASN1Integer (signature[0])); seq.addObject (new ASN1Integer (signature[1])); seq.close (); return s.toByteArray (); } catch ( IOException e ) { } return null; }
Example #11
Source File: BCECUtilTest.java From gmhelper with Apache License 2.0 | 6 votes |
@Test public void testECPrivateKeyPKCS8() { try { AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter(); ECPrivateKeyParameters priKeyParams = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters pubKeyParams = (ECPublicKeyParameters) keyPair.getPublic(); byte[] pkcs8Bytes = BCECUtil.convertECPrivateKeyToPKCS8(priKeyParams, pubKeyParams); BCECPrivateKey priKey = BCECUtil.convertPKCS8ToECPrivateKey(pkcs8Bytes); byte[] sign = SM2Util.sign(priKey, GMBaseTest.WITH_ID, GMBaseTest.SRC_DATA); System.out.println("SM2 sign with withId result:\n" + ByteUtils.toHexString(sign)); boolean flag = SM2Util.verify(pubKeyParams, GMBaseTest.WITH_ID, GMBaseTest.SRC_DATA, sign); if (!flag) { Assert.fail("[withId] verify failed"); } } catch (Exception e) { e.printStackTrace(); Assert.fail(); } }
Example #12
Source File: SM2UtilTest.java From gmhelper with Apache License 2.0 | 6 votes |
@Test public void testGenerateBCECKeyPair() { try { KeyPair keyPair = SM2Util.generateKeyPair(); ECPrivateKeyParameters priKey = BCECUtil.convertPrivateKeyToParameters((BCECPrivateKey) keyPair.getPrivate()); ECPublicKeyParameters pubKey = BCECUtil.convertPublicKeyToParameters((BCECPublicKey) keyPair.getPublic()); byte[] sign = SM2Util.sign(priKey, WITH_ID, SRC_DATA); boolean flag = SM2Util.verify(pubKey, WITH_ID, SRC_DATA, sign); if (!flag) { Assert.fail("verify failed"); } sign = SM2Util.sign(priKey, SRC_DATA); flag = SM2Util.verify(pubKey, SRC_DATA, sign); if (!flag) { Assert.fail("verify failed"); } } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } }
Example #13
Source File: Sm2KeyExchangeUtil.java From littleca with Apache License 2.0 | 6 votes |
/** * * @param initiator true表示发起方,false表示响应方 * @param keyBits 生成的密钥长度 * @param confirmationTag 确认信息,如果是响应方可以为null;如果是发起方则应为响应方的s1 * @param selfStaticPriv 己方固定私钥 * @param selfEphemeralPriv 己方临时私钥 * @param selfId 己方ID * @param otherStaticPub 对方固定公钥 * @param otherEphemeralPub 对方临时公钥 * @param otherId 对方ID * @return */ public static ExchangeResult calculateKeyWithConfirmation(boolean initiator, int keyBits, byte[] confirmationTag, ECPrivateKeyParameters selfStaticPriv, ECPrivateKeyParameters selfEphemeralPriv, byte[] selfId, ECPublicKeyParameters otherStaticPub, ECPublicKeyParameters otherEphemeralPub, byte[] otherId) { SM2KeyExchange exch = new SM2KeyExchange(); exch.init(new ParametersWithID( new SM2KeyExchangePrivateParameters(initiator, selfStaticPriv, selfEphemeralPriv), selfId)); byte[][] result = exch.calculateKeyWithConfirmation( keyBits, confirmationTag, new ParametersWithID(new SM2KeyExchangePublicParameters(otherStaticPub, otherEphemeralPub), otherId)); ExchangeResult confirmResult = new ExchangeResult(); confirmResult.setKey(result[0]); if (initiator) { confirmResult.setS2(result[1]); } else { confirmResult.setS1(result[1]); confirmResult.setS2(result[2]); } return confirmResult; }
Example #14
Source File: SM2UtilTest.java From gmhelper with Apache License 2.0 | 6 votes |
@Test public void testEncodeSM2CipherToDER_C1C2C3() { try { AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter(); ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic(); byte[] encryptedData = SM2Util.encrypt(Mode.C1C2C3, pubKey, SRC_DATA); byte[] derCipher = SM2Util.encodeSM2CipherToDER(Mode.C1C2C3, encryptedData); FileUtil.writeFile("target/derCipher_c1c2c3.dat", derCipher); byte[] decryptedData = SM2Util.decrypt(Mode.C1C2C3, priKey, SM2Util.decodeDERSM2Cipher(Mode.C1C2C3, derCipher)); if (!Arrays.equals(decryptedData, SRC_DATA)) { Assert.fail(); } Assert.assertTrue(true); } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } }
Example #15
Source File: BouncyCastleCrypto.java From fabric-api-archive with Apache License 2.0 | 6 votes |
@Override public byte[] sign(byte[] hash, byte[] privateKey) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(new BigInteger(privateKey), domain)); BigInteger[] signature = signer.generateSignature(hash); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(toCanonicalS(signature[1]))); seq.close(); return baos.toByteArray(); } catch (IOException e) { return new byte[0]; } }
Example #16
Source File: SM2UtilTest.java From gmhelper with Apache License 2.0 | 6 votes |
@Test public void testEncodeSM2CipherToDER() { try { AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter(); ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic(); byte[] encryptedData = SM2Util.encrypt(pubKey, SRC_DATA); byte[] derCipher = SM2Util.encodeSM2CipherToDER(encryptedData); FileUtil.writeFile("target/derCipher.dat", derCipher); byte[] decryptedData = SM2Util.decrypt(priKey, SM2Util.decodeDERSM2Cipher(derCipher)); if (!Arrays.equals(decryptedData, SRC_DATA)) { Assert.fail(); } Assert.assertTrue(true); } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } }
Example #17
Source File: SM2UtilTest.java From gmhelper with Apache License 2.0 | 6 votes |
@Test public void testSM2KeyGen2() { try { AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter(); ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic(); System.out.println("Pri Hex:" + ByteUtils.toHexString(priKey.getD().toByteArray()).toUpperCase()); System.out.println("Pub X Hex:" + ByteUtils.toHexString(pubKey.getQ().getAffineXCoord().getEncoded()).toUpperCase()); System.out.println("Pub X Hex:" + ByteUtils.toHexString(pubKey.getQ().getAffineYCoord().getEncoded()).toUpperCase()); System.out.println("Pub Point Hex:" + ByteUtils.toHexString(pubKey.getQ().getEncoded(false)).toUpperCase()); } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } }
Example #18
Source File: SM2Signer.java From web3sdk with Apache License 2.0 | 5 votes |
public BigInteger[] generateSignature2() throws CryptoException { byte[] eHash = digestDoFinal(); BigInteger n = ecParams.getN(); BigInteger e = calculateE(eHash); BigInteger d = ((ECPrivateKeyParameters) ecKey).getD(); BigInteger r, s; ECMultiplier basePointMultiplier = createBasePointMultiplier(); // 5.2.1 Draft RFC: SM2 Public Key Algorithms do // generate s { BigInteger k; do // generate r { // A3 k = kCalculator.nextK(); // A4 ECPoint p = basePointMultiplier.multiply(ecParams.getG(), k).normalize(); // A5 r = e.add(p.getAffineXCoord().toBigInteger()).mod(n); } while (r.equals(ZERO) || r.add(k).equals(n)); // A6 BigInteger dPlus1ModN = d.add(ONE).modInverse(n); s = k.subtract(r.multiply(d)).mod(n); s = dPlus1ModN.multiply(s).mod(n); } while (s.equals(ZERO)); return new BigInteger[] {r, s}; }
Example #19
Source File: ECIESUtil.java From nuls-v2 with MIT License | 5 votes |
private static byte[] deriveSharedSecret(ECPrivateKeyParameters priKeyParaA, byte[] pubKeyB) { ECDHBasicAgreement agreement = new ECDHBasicAgreement(); agreement.init(priKeyParaA); ECPublicKeyParameters pubKeyParaB = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pubKeyB), CURVE); BigInteger result = agreement.calculateAgreement(pubKeyParaB); byte[] sharedSecret = BigIntegers.asUnsignedByteArray(agreement.getFieldSize(), result); return sharedSecret; }
Example #20
Source File: ECIESUtil.java From nuls-v2 with MIT License | 5 votes |
private static byte[] deriveSharedSecret(byte[] priKeyA, ECPublicKeyParameters pubKeyParaB) { ECPrivateKeyParameters priKeyParaA = new ECPrivateKeyParameters(new BigInteger(1, priKeyA), CURVE); ECDHBasicAgreement agreement = new ECDHBasicAgreement(); agreement.init(priKeyParaA); BigInteger result = agreement.calculateAgreement(pubKeyParaB); byte[] sharedSecret = BigIntegers.asUnsignedByteArray(agreement.getFieldSize(), result); return sharedSecret; }
Example #21
Source File: BCECUtil.java From littleca with Apache License 2.0 | 5 votes |
/** * openssl d2i_ECPrivateKey函数要求的DER编码的私钥也是PKCS1标准的, * 这个工具函数的主要目的就是为了能生成一个openssl可以“识别”的ECC私钥 * * @param priKey * @param pubKey * @return * @throws IOException */ public static byte[] convertEcPriKeyToPkcs1Der(ECPrivateKeyParameters priKey, ECPublicKeyParameters pubKey) throws IOException { byte[] pkcs8Bytes = convertEcPriKeyToPkcs8Der(priKey, pubKey); PrivateKeyInfo pki = PrivateKeyInfo.getInstance(pkcs8Bytes); ASN1Encodable encodable = pki.parsePrivateKey(); ASN1Primitive primitive = encodable.toASN1Primitive(); byte[] pkcs1Bytes = primitive.getEncoded(); return pkcs1Bytes; }
Example #22
Source File: Sm2KeyPairImpl.java From littleca with Apache License 2.0 | 5 votes |
public Sm2KeyPairImpl(boolean selfgen) { SecureRandom random = new SecureRandom(); ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(DOMAIN_PARAMS, random); ECKeyPairGenerator keyGen = new ECKeyPairGenerator(); keyGen.init(keyGenerationParams); AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair(); ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic(); ECDomainParameters domainParams = priKey.getParameters(); ECParameterSpec spec = new ECParameterSpec(domainParams.getCurve(), domainParams.getG(), domainParams.getN(), domainParams.getH()); BCECPublicKey bcecPublicKey = new BCECPublicKey(ALGO_NAME_EC, pubKey, spec, BouncyCastleProvider.CONFIGURATION); publicKey = new Sm2PublicKeyImpl(bcecPublicKey); privateKey = new Sm2PrivateKeyImpl(new BCECPrivateKey(ALGO_NAME_EC, priKey, bcecPublicKey, spec, BouncyCastleProvider.CONFIGURATION)); }
Example #23
Source File: ECDSASigner.java From web3sdk with Apache License 2.0 | 5 votes |
/** * The same generateSignature with the temporary variable ECPoint P generated by the signature * process is also returned together * * @param message the message that will be verified later. */ public Object[] generateSignature2(byte[] message) { ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); BigInteger e = calculateE(n, message); BigInteger d = ((ECPrivateKeyParameters) key).getD(); if (kCalculator.isDeterministic()) { kCalculator.init(n, d, message); } else { kCalculator.init(n, random); } BigInteger r, s; /** */ ECPoint p; ECMultiplier basePointMultiplier = createBasePointMultiplier(); // 5.3.2 do // generate s { BigInteger k; do // generate r { k = kCalculator.nextK(); p = basePointMultiplier.multiply(ec.getG(), k).normalize(); // 5.3.3 r = p.getAffineXCoord().toBigInteger().mod(n); } while (r.equals(ZERO)); s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n); } while (s.equals(ZERO)); return new Object[] {r, s, p}; }
Example #24
Source File: ECKeyPair.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
/** * Sign a hash with the private key of this key pair. * @param transactionHash the hash to sign * @return An {@link ECDSASignature} of the hash */ public ECDSASignature sign(byte[] transactionHash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(transactionHash); return new ECDSASignature(components[0], components[1]).toCanonicalised(); }
Example #25
Source File: ECKeyPair.java From web3sdk with Apache License 2.0 | 5 votes |
/** * Sign a hash with the private key of this key pair. * * @param hash the hash to sign * @return An {@link ECDSASignature} of the hash */ public ECDSASignature sign(byte[] hash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(hash); return new ECDSASignature(components[0], components[1]).toCanonicalised(); }
Example #26
Source File: Cipher.java From nuls with MIT License | 5 votes |
public ECPoint initEnc(SM2 sm2, ECPoint userKey) { AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.generateKeyPair(); ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) key.getPrivate(); ECPublicKeyParameters ecpub = (ECPublicKeyParameters) key.getPublic(); BigInteger k = ecpriv.getD(); ECPoint c1 = ecpub.getQ(); this.p2 = userKey.multiply(k); reset(); return c1; }
Example #27
Source File: ECKeyPair.java From web3j with Apache License 2.0 | 5 votes |
/** * Sign a hash with the private key of this key pair. * * @param transactionHash the hash to sign * @return An {@link ECDSASignature} of the hash */ public ECDSASignature sign(byte[] transactionHash) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); signer.init(true, privKey); BigInteger[] components = signer.generateSignature(transactionHash); return new ECDSASignature(components[0], components[1]).toCanonicalised(); }
Example #28
Source File: ECKeyPair.java From WalletCordova with GNU Lesser General Public License v2.1 | 5 votes |
public static ECKeyPair createNew (boolean compressed) { ECKeyPairGenerator generator = new ECKeyPairGenerator (); ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters (domain, secureRandom); generator.init (keygenParams); AsymmetricCipherKeyPair keypair = generator.generateKeyPair (); ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate (); ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic (); ECKeyPair k = new ECKeyPair (); k.priv = privParams.getD (); k.compressed = compressed; k.pub = pubParams.getQ ().getEncoded (compressed); return k; }
Example #29
Source File: RLPxConnectionFactory.java From incubator-tuweni with Apache License 2.0 | 5 votes |
private static EthereumIESEncryptionEngine forDecryption( SecretKey privateKey, PublicKey ephemeralPublicKey, Bytes iv, Bytes commonMac) { CipherParameters pubParam = new ECPublicKeyParameters(ephemeralPublicKey.asEcPoint(), CURVE); CipherParameters privParam = new ECPrivateKeyParameters(privateKey.bytes().toUnsignedBigInteger(), CURVE); BasicAgreement agreement = new ECDHBasicAgreement(); agreement.init(privParam); byte[] agreementValue = BigIntegers.asUnsignedByteArray(agreement.getFieldSize(), agreement.calculateAgreement(pubParam)); IESWithCipherParameters iesWithCipherParameters = new IESWithCipherParameters(new byte[0], new byte[0], 128, 128); EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction kdf = new EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction(1, new SHA256Digest()); kdf.init(new KDFParameters(agreementValue, iesWithCipherParameters.getDerivationV())); EthereumIESEncryptionEngine engine = new EthereumIESEncryptionEngine( agreement, kdf, new HMac(new SHA256Digest()), commonMac.toArrayUnsafe(), new BufferedBlockCipher(new SICBlockCipher(new AESEngine()))); ParametersWithIV cipherParameters = new ParametersWithIV(iesWithCipherParameters, iv.toArrayUnsafe()); engine.init(false, privParam, pubParam, cipherParameters); return engine; }
Example #30
Source File: ECDSASigner.java From web3sdk with Apache License 2.0 | 5 votes |
/** * generate a signature for the given message using the key we were initialised with. For * conventional DSA the message should be a SHA-1 hash of the message of interest. * * @param message the message that will be verified later. */ @Override public BigInteger[] generateSignature(byte[] message) { ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); BigInteger e = calculateE(n, message); BigInteger d = ((ECPrivateKeyParameters) key).getD(); if (kCalculator.isDeterministic()) { kCalculator.init(n, d, message); } else { kCalculator.init(n, random); } BigInteger r, s; ECMultiplier basePointMultiplier = createBasePointMultiplier(); // 5.3.2 do // generate s { BigInteger k; do // generate r { k = kCalculator.nextK(); ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize(); // 5.3.3 r = p.getAffineXCoord().toBigInteger().mod(n); } while (r.equals(ZERO)); s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n); } while (s.equals(ZERO)); return new BigInteger[] {r, s}; }