Search in sources :

Example 1 with P12Manager

use of org.fisco.bcos.channel.client.P12Manager in project web3sdk by FISCO-BCOS.

the class AccountTest method accountTest.

@Test
public void accountTest() throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-keystore-sample.xml");
    // test p12
    P12Manager p12 = context.getBean(P12Manager.class);
    ECKeyPair p12KeyPair = p12.getECKeyPair();
    assertEquals(p12KeyPair.getPrivateKey().toString(16), PRIVATE_KEY);
    assertEquals(p12KeyPair.getPublicKey().toString(16), PUBLIC_KEY);
    ECPublicKey publicKey = (ECPublicKey) p12.getPublicKey();
    byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
    assertEquals(publicKeyValue.toString(16), PUBLIC_KEY);
    Credentials credentials = Credentials.create(p12KeyPair);
    assertEquals(credentials.getAddress(), ADDRESS);
    // test pem
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    assertEquals(pemKeyPair.getPrivateKey().toString(16), PRIVATE_KEY);
    assertEquals(pemKeyPair.getPublicKey().toString(16), PUBLIC_KEY);
    Credentials credentialsPEM = Credentials.create(pemKeyPair);
    assertEquals(credentialsPEM.getAddress(), ADDRESS);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) PEMManager(org.fisco.bcos.channel.client.PEMManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) P12Manager(org.fisco.bcos.channel.client.P12Manager) ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) BigInteger(java.math.BigInteger) Credentials(org.fisco.bcos.web3j.crypto.Credentials) Test(org.junit.Test)

Example 2 with P12Manager

use of org.fisco.bcos.channel.client.P12Manager in project web3sdk by FISCO-BCOS.

the class ECKeyPairTest method encryptECKeyPairTestWithPem.

@Test
public void encryptECKeyPairTestWithPem() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-keystore-sample.xml");
    // test p12
    P12Manager p12 = context.getBean(P12Manager.class);
    ECKeyPair ecKeyPair = p12.getECKeyPair();
    ECCEncrypt encrypt = new ECCEncrypt(ecKeyPair.getPublicKey());
    ECCDecrypt decrypt = new ECCDecrypt(ecKeyPair.getPrivateKey());
    String message = "";
    byte[] encryptData = encrypt.encrypt(message.getBytes("utf-8"));
    byte[] decryptData = decrypt.decrypt(encryptData);
    assertEquals(message, new String(decryptData, "utf-8"));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) P12Manager(org.fisco.bcos.channel.client.P12Manager) ECCDecrypt(org.fisco.bcos.web3j.crypto.tool.ECCDecrypt) ECCEncrypt(org.fisco.bcos.web3j.crypto.tool.ECCEncrypt) Test(org.junit.Test)

Example 3 with P12Manager

use of org.fisco.bcos.channel.client.P12Manager in project web3sdk by FISCO-BCOS.

the class ECKeyPairTest method verifySecp256ECDSASignTest0.

@Test
public void verifySecp256ECDSASignTest0() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-keystore-sample.xml");
    // test p12
    P12Manager p12 = context.getBean(P12Manager.class);
    ECKeyPair ecKeyPair = p12.getECKeyPair();
    ECDSASign ecdsaSign = new ECDSASign();
    String message = "message";
    Sign.SignatureData signatureData = ecdsaSign.secp256SignMessage(message.getBytes(), ecKeyPair);
    SHA3Digest sha3Digest = new SHA3Digest();
    byte[] hash = sha3Digest.hash(message.getBytes());
    boolean verify = ecdsaSign.secp256Verify(hash, ecKeyPair.getPublicKey(), signatureData);
    assertEquals(verify, true);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) P12Manager(org.fisco.bcos.channel.client.P12Manager) Test(org.junit.Test)

Example 4 with P12Manager

use of org.fisco.bcos.channel.client.P12Manager in project web3sdk by FISCO-BCOS.

the class ECKeyPairTest method verifyECDSASignTest0.

@Test
public void verifyECDSASignTest0() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-keystore-sample.xml");
    // test p12
    P12Manager p12 = context.getBean(P12Manager.class);
    ECKeyPair ecKeyPair = p12.getECKeyPair();
    ECDSASign ecdsaSign = new ECDSASign();
    String message = "message";
    Sign.SignatureData signatureData = ecdsaSign.signMessage(message.getBytes(), ecKeyPair);
    SHA3Digest sha3Digest = new SHA3Digest();
    byte[] hash = sha3Digest.hash(message.getBytes());
    boolean verify = ecdsaSign.verify(hash, ecKeyPair.getPublicKey(), signatureData);
    assertEquals(verify, true);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) P12Manager(org.fisco.bcos.channel.client.P12Manager) Test(org.junit.Test)

Example 5 with P12Manager

use of org.fisco.bcos.channel.client.P12Manager in project web3sdk by FISCO-BCOS.

the class ECKeyPairTest method verifyECKeyPairTestWithPem.

@Test
public void verifyECKeyPairTestWithPem() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-keystore-sample.xml");
    // test p12
    P12Manager p12 = context.getBean(P12Manager.class);
    ECKeyPair ecKeyPair = p12.getECKeyPair();
    String message = "ecc encrypt test";
    ECDSASignature signature = ecKeyPair.sign(message.getBytes());
    boolean verify = ecKeyPair.verify(message.getBytes(), signature);
    assertEquals(verify, true);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) P12Manager(org.fisco.bcos.channel.client.P12Manager) Test(org.junit.Test)

Aggregations

P12Manager (org.fisco.bcos.channel.client.P12Manager)5 Test (org.junit.Test)5 ApplicationContext (org.springframework.context.ApplicationContext)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 BigInteger (java.math.BigInteger)1 ECPublicKey (org.bouncycastle.jce.interfaces.ECPublicKey)1 PEMManager (org.fisco.bcos.channel.client.PEMManager)1 Credentials (org.fisco.bcos.web3j.crypto.Credentials)1 ECKeyPair (org.fisco.bcos.web3j.crypto.ECKeyPair)1 ECCDecrypt (org.fisco.bcos.web3j.crypto.tool.ECCDecrypt)1 ECCEncrypt (org.fisco.bcos.web3j.crypto.tool.ECCEncrypt)1