Search in sources :

Example 16 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class MerkleHashDataSetTest method createCryptoSetting.

private CryptoSetting createCryptoSetting() {
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setSupportedProviders(supportedProviders);
    cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256);
    cryptoConfig.setAutoVerifyHash(true);
    return cryptoConfig;
}
Also used : CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig)

Example 17 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class LedgerInitConfiguration method createCryptoConfig.

private static CryptoConfig createCryptoConfig(CryptoProperties cryptoProperties) {
    Set<String> cryptoProviderNames = new LinkedHashSet<String>();
    if (cryptoProperties.getProviders() != null) {
        for (String providerName : cryptoProperties.getProviders()) {
            cryptoProviderNames.add(providerName);
        }
    }
    CryptoProvider[] cryptoProviders = new CryptoProvider[cryptoProviderNames.size()];
    int i = 0;
    for (String providerName : cryptoProviderNames) {
        cryptoProviders[i] = Crypto.getProvider(providerName);
        i++;
    }
    String hashAlgorithmName = StringUtils.trim(cryptoProperties.getHashAlgorithm());
    hashAlgorithmName = hashAlgorithmName.length() == 0 ? DEFAULT_HASH_ALGORITHM : hashAlgorithmName;
    CryptoAlgorithm hashAlgorithm = Crypto.getAlgorithm(hashAlgorithmName);
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setSupportedProviders(cryptoProviders);
    cryptoConfig.setAutoVerifyHash(cryptoProperties.isVerifyHash());
    cryptoConfig.setHashAlgorithm(hashAlgorithm);
    return cryptoConfig;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoAlgorithm(com.jd.blockchain.crypto.CryptoAlgorithm) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig)

Example 18 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class CryptoConfig method setSupportedProviders.

public void setSupportedProviders(CryptoProvider[] supportedProviders) {
    HashMap<String, CryptoProvider> providers = new HashMap<String, CryptoProvider>();
    HashMap<String, CryptoAlgorithm> nameAlgorithms = new HashMap<String, CryptoAlgorithm>();
    HashMap<Short, CryptoAlgorithm> codeAlgorithms = new HashMap<Short, CryptoAlgorithm>();
    if (supportedProviders != null) {
        // 检查是否存在重复的提供者以及算法;
        for (CryptoProvider cryptoProvider : supportedProviders) {
            if (providers.containsKey(cryptoProvider.getName())) {
                throw new LedgerException("Duplicate crypto providers [" + cryptoProvider.getName() + "]!");
            }
            CryptoAlgorithm[] algorithms = cryptoProvider.getAlgorithms();
            for (CryptoAlgorithm alg : algorithms) {
                if (nameAlgorithms.containsKey(alg.name())) {
                    throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider " + cryptoProvider.getName() + "!");
                }
                if (codeAlgorithms.containsKey(alg.code())) {
                    throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider" + cryptoProvider.getName() + "!");
                }
                nameAlgorithms.put(alg.name(), alg);
                codeAlgorithms.put(alg.code(), alg);
            }
            providers.put(cryptoProvider.getName(), cryptoProvider);
        }
    }
    this.providers = providers;
    this.nameAlgorithms = nameAlgorithms;
    this.codeAlgorithms = codeAlgorithms;
    this.cryptoProviders = supportedProviders;
}
Also used : HashMap(java.util.HashMap) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) LedgerException(com.jd.blockchain.ledger.LedgerException) CryptoAlgorithm(com.jd.blockchain.crypto.CryptoAlgorithm)

Aggregations

CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)18 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)15 Test (org.junit.Test)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)6 Bytes (utils.Bytes)6 HashDigest (com.jd.blockchain.crypto.HashDigest)5 Random (java.util.Random)4 CryptoAlgorithm (com.jd.blockchain.crypto.CryptoAlgorithm)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)3 MerkleHashDataset (com.jd.blockchain.ledger.core.MerkleHashDataset)3 LedgerException (com.jd.blockchain.ledger.LedgerException)2 LedgerSettings (com.jd.blockchain.ledger.LedgerSettings)2 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)2 LedgerConfiguration (com.jd.blockchain.ledger.core.LedgerConfiguration)2 ConsensusParticipantData (com.jd.blockchain.transaction.ConsensusParticipantData)2 LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 NetworkAddress (utils.net.NetworkAddress)2 AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)1