当前位置: 首页>>代码示例>>Java>>正文


Java SecureRandom.getInstanceStrong方法代码示例

本文整理汇总了Java中java.security.SecureRandom.getInstanceStrong方法的典型用法代码示例。如果您正苦于以下问题:Java SecureRandom.getInstanceStrong方法的具体用法?Java SecureRandom.getInstanceStrong怎么用?Java SecureRandom.getInstanceStrong使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.SecureRandom的用法示例。


在下文中一共展示了SecureRandom.getInstanceStrong方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: EncryptionPayload

import java.security.SecureRandom; //导入方法依赖的package包/类
@JsonCreator
public EncryptionPayload(@JsonProperty("value") SecretValue value,
                         @JsonProperty("userdata") Optional<UserData> userData,
                         @JsonProperty("created") ZonedDateTime created,
                         Optional<UserAlias> createdBy,
                         @JsonProperty("modified") ZonedDateTime modified,
                         Optional<UserAlias> modifiedBy,
                         @JsonProperty("comment") Optional<Comment> comment) {

    this.value = value;
    this.userData = userData;
    this.created = created;
    this.modified = modified;
    this.createdBy = createdBy;
    this.modifiedBy = modifiedBy;
    this.comment = comment;

    try {
        this.random = SecureRandom.getInstanceStrong();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Failed to instantiate random number generator", e);
    }
}
 
开发者ID:schibsted,项目名称:strongbox,代码行数:24,代码来源:EncryptionPayload.java

示例2: testAddRemove_SingleInstance

import java.security.SecureRandom; //导入方法依赖的package包/类
@Test
public void testAddRemove_SingleInstance() throws InterruptedException, NoSuchAlgorithmException {
    final String name = "testAddNegative_SingleInstance";

    RedissonClient r = BaseTest.createInstance();
    RSortedSet<Integer> map = r.getSortedSet(name);
    map.clear();
    int length = 1000;
    for (int i = 0; i < length; i++) {
        map.add(i);
    }

    final AtomicInteger counter = new AtomicInteger(length);
    final Random rnd = SecureRandom.getInstanceStrong();
    testSingleInstanceConcurrency(length, rc -> {
        RSortedSet<Integer> set = rc.getSortedSet(name);
        int c = counter.incrementAndGet();
        Assert.assertTrue(set.add(c));
        set.remove(rnd.nextInt(length));
    });

    Assert.assertEquals(counter.get(), length*2);
    
    Integer prevVal = null;
    for (Integer val : map) {
        if (prevVal == null) {
            prevVal = val;
            continue;
        }
        if (val < prevVal) {
            Assert.fail();
        }
    }
    
    r.shutdown();
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:37,代码来源:ConcurrentRedissonSortedSetTest.java

示例3: getSecureRandom

import java.security.SecureRandom; //导入方法依赖的package包/类
private static SecureRandom getSecureRandom(boolean strong) {
    if (!strong) {
        return new SecureRandom();
    }

    try {
        return SecureRandom.getInstanceStrong();
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeCryptoException(
                "could not get strong SecureRandom: " + ex.getMessage());
    }
}
 
开发者ID:xipki,项目名称:xitk,代码行数:13,代码来源:SecurityFactoryImpl.java

示例4: generateToken

import java.security.SecureRandom; //导入方法依赖的package包/类
/**
 * Generate a strong random 16 byte token using the system provided {@link SecureRandom}.
 *
 * @return a random 16 byte token
 */
static byte[] generateToken() {

  try {
    SecureRandom sr = SecureRandom.getInstanceStrong();
    byte[] token = new byte[16];
    sr.nextBytes(token);
    return token;
  } catch (NoSuchAlgorithmException nsa) {
    throw new InterledgerRuntimeException("Could not generate token", nsa);
  }
}
 
开发者ID:hyperledger,项目名称:quilt,代码行数:17,代码来源:PskContext.java

示例5: seed

import java.security.SecureRandom; //导入方法依赖的package包/类
/**
 * Constructs an instance of the header with a randomly generated value.
 *
 * @return new nonce header
 */
public static PskNonceHeader seed() {
  try {
    SecureRandom sr = SecureRandom.getInstanceStrong();
    byte[] nonce = new byte[16];
    sr.nextBytes(nonce);
    return new PskNonceHeader(nonce);
  } catch (NoSuchAlgorithmException nsa) {
    throw new InterledgerRuntimeException("Could not generate secure nonce", nsa);
  }
}
 
开发者ID:hyperledger,项目名称:quilt,代码行数:16,代码来源:PskNonceHeader.java

示例6: test256bitKey

import java.security.SecureRandom; //导入方法依赖的package包/类
@Test
public final void test256bitKey()
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
    InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

  SecureRandom sr = SecureRandom.getInstanceStrong();
  byte[] nonce = new byte[16];
  sr.nextBytes(nonce);

  KeyGenerator keygen = KeyGenerator.getInstance("AES");
  keygen.init(256);
  byte[] key = keygen.generateKey().getEncoded();

  byte[] data = new byte[256];
  sr.nextBytes(data);

  Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");
  GCMParameterSpec paramSpec = new GCMParameterSpec(128, nonce);

  try {
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), paramSpec);
    cipher.doFinal(data);
  } catch (InvalidKeyException e) {
    throw new InterledgerRuntimeException("Error loading 256bit key. "
        + "Likley cause is missing Unlimited Strength Jurisdiction Policy Files.", e);
  }

}
 
开发者ID:hyperledger,项目名称:quilt,代码行数:29,代码来源:Aes256BitKeysizeTest.java

示例7: createSecureRandom

import java.security.SecureRandom; //导入方法依赖的package包/类
private static Random createSecureRandom() {
  try {
    return SecureRandom.getInstanceStrong();
  } catch (NoSuchAlgorithmException nae) {
    LOGGER.warn("Couldn't create strong secure random generator; reason: {}.", nae.getMessage());
    return new SecureRandom();
  }
}
 
开发者ID:springuni,项目名称:springuni-particles,代码行数:9,代码来源:RandomUtil.java

示例8: SecureRandomEntropySource

import java.security.SecureRandom; //导入方法依赖的package包/类
public SecureRandomEntropySource() {
    try {
        this.secureRandom = SecureRandom.getInstanceStrong();
        /*
         * Right after the SecureRandom constructor, perform a single nextBytes with some small numBytes > 0
         * and disregard the result (even though 0 should do if a Vulcan implemented the spec), then setSeed()
         * with whatever unpredictable data at hand, then use nextBytes.
         *
         * https://crypto.stackexchange.com/a/51222/44838
         */
        secureRandom.nextBytes(new byte[4]);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("could not get strong secure random instace", e);
    }
}
 
开发者ID:patrickfav,项目名称:dice,代码行数:16,代码来源:SecureRandomEntropySource.java

示例9: main

import java.security.SecureRandom; //导入方法依赖的package包/类
public static void main(final String[] argv) throws NoSuchAlgorithmException, IOException {
    final NetworkParameters params = new MainNetParams();
    final LocalTransactionSigner signer = new LocalTransactionSigner();
    final SecureRandom secureRandom = SecureRandom.getInstanceStrong();
    final ECKey key = new ECKey(secureRandom);
    final KeyBag bag = new KeyBag() {
        @Override
        public ECKey findKeyFromPubHash(byte[] pubkeyHash) {
            return key;
        }

        @Override
        public ECKey findKeyFromPubKey(byte[] pubkey) {
            return key;
        }

        @Override
        public RedeemData findRedeemDataFromScriptHash(byte[] scriptHash) {
            return null;
        }

    };

    // Generate a fictional output transaction we take values from, and
    // an input transaction for the test case

    final Transaction outputTransaction = new Transaction(params);
    final Transaction inputTransaction = new Transaction(params);
    final TransactionOutput output = new TransactionOutput(params, inputTransaction, Coin.ZERO, key.toAddress(params));

    inputTransaction.addOutput(output);
    outputTransaction.addInput(output);
    outputTransaction.addOutput(Coin.ZERO, new ECKey(secureRandom).toAddress(params));

    addOutputs(outputTransaction, bag);

    // Sign the transaction
    final ProposedTransaction proposedTransaction = new ProposedTransaction(outputTransaction);
    signer.signInputs(proposedTransaction, bag);
    final TransactionInput input = proposedTransaction.partialTx.getInput(0);

    input.verify(output);
    input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(),
        EnumSet.of(Script.VerifyFlag.DERSIG, Script.VerifyFlag.P2SH));

    final Script scriptSig = input.getScriptSig();
    final TransactionSignature signature = TransactionSignature.decodeFromBitcoin(scriptSig.getChunks().get(0).data, true, false);

    // First output a conventional low-S transaction with the LOW_S flag, for the tx_valid.json set
    System.out.println("[\"A transaction with a low-S signature.\"],");
    System.out.println("[[[\""
        + inputTransaction.getHashAsString() + "\", "
        + output.getIndex() + ", \""
        + scriptToString(output.getScriptPubKey()) + "\"]],\n"
        + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \""
        + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],");

    final BigInteger highS = HIGH_S_DIFFERENCE.subtract(signature.s);
    final TransactionSignature highSig = new TransactionSignature(signature.r, highS);
    input.setScriptSig(new ScriptBuilder().data(highSig.encodeToBitcoin()).data(scriptSig.getChunks().get(1).data).build());
    input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(),
        EnumSet.of(Script.VerifyFlag.P2SH));

    // A high-S transaction without the LOW_S flag, for the tx_valid.json set
    System.out.println("[\"A transaction with a high-S signature.\"],");
    System.out.println("[[[\""
        + inputTransaction.getHashAsString() + "\", "
        + output.getIndex() + ", \""
        + scriptToString(output.getScriptPubKey()) + "\"]],\n"
        + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \""
        + Script.VerifyFlag.P2SH.name() + "\"],");

    // Lastly a conventional high-S transaction with the LOW_S flag, for the tx_invalid.json set
    System.out.println("[\"A transaction with a high-S signature.\"],");
    System.out.println("[[[\""
        + inputTransaction.getHashAsString() + "\", "
        + output.getIndex() + ", \""
        + scriptToString(output.getScriptPubKey()) + "\"]],\n"
        + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \""
        + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],");
}
 
开发者ID:creativechain,项目名称:creacoinj,代码行数:82,代码来源:GenerateLowSTests.java

示例10: initializeRandom

import java.security.SecureRandom; //导入方法依赖的package包/类
/**
 * Initializes the random source.
 *
 * @throws NoSuchAlgorithmException
 *             this should not happen.
 */
@PostConstruct
public void initializeRandom() throws NoSuchAlgorithmException {

    random = SecureRandom.getInstanceStrong();
}
 
开发者ID:trajano,项目名称:app-ms,代码行数:12,代码来源:TokenGenerator.java


注:本文中的java.security.SecureRandom.getInstanceStrong方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。