Java Code Examples for org.bouncycastle.util.Arrays#concatenate()

The following examples show how to use org.bouncycastle.util.Arrays#concatenate() . 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: EthereumIESEncryptionEngine.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {

    // Compute the common value and convert to byte array.
    agree.init(privParam);
    BigInteger z = agree.calculateAgreement(pubParam);
    byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);

    // Create input to KDF.
    if (V.length != 0) {
      byte[] VZ = Arrays.concatenate(V, Z);
      Arrays.fill(Z, (byte) 0);
      Z = VZ;
    }

    try {
      // Initialise the KDF.
      KDFParameters kdfParam = new KDFParameters(Z, param.getDerivationV());
      kdf.init(kdfParam);

      return forEncryption ? encryptBlock(in, inOff, inLen) : decryptBlock(in, inOff, inLen);
    } finally {
      Arrays.fill(Z, (byte) 0);
    }
  }
 
Example 2
Source File: Cable.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
public CableSessionData generateSessionData(CablePairingData pairingData) {
  byte[] nonce = new byte[8];
  random.nextBytes(nonce);

  byte[] clientEidHash = Crypto.hmacSha256(pairingData.irk,
      Arrays.concatenate(nonce, HMAC_TAG_CLIENT_EID), 8);
  byte[] clientEid = Arrays.concatenate(nonce, clientEidHash);

  byte[] authenticatorEid = Crypto.hmacSha256(pairingData.irk,
      Arrays.concatenate(clientEid, HMAC_TAG_AUTHENTICATOR_EID), 16);

  byte[] sessionPreKey = Crypto.hkdfSha256(pairingData.lk, nonce,
      HKDF_INFO_SESSION_PRE_KEY, 32);

  return new CableSessionData(pairingData.version, clientEid, authenticatorEid, sessionPreKey);
}
 
Example 3
Source File: XTSAESBlockCipherTest.java    From InflatableDonkey with MIT License 6 votes vote down vote up
public XTSAESBlockCipherTest() throws IOException {
    // Key = key1 | key2
    byte[] keyData = Arrays.concatenate(VECTOR_4.key1(), VECTOR_4.key2());
    key = new KeyParameter(keyData);
    dataUnitLength = VECTOR_4.ctx().length;

    // Vectors 4, 5, 6 are sequential 512 byte data units starting from data unit sequence number 0.
    ByteArrayOutputStream ptxs = new ByteArrayOutputStream();
    ptxs.write(VECTOR_4.ptx());
    ptxs.write(VECTOR_5.ptx());
    ptxs.write(VECTOR_6.ptx());
    ptx = ptxs.toByteArray();

    ByteArrayOutputStream ctxs = new ByteArrayOutputStream();
    ctxs.write(VECTOR_4.ctx());
    ctxs.write(VECTOR_5.ctx());
    ctxs.write(VECTOR_6.ctx());
    ctx = ctxs.toByteArray();
}
 
Example 4
Source File: Algorithm5.java    From sambox with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] computePassword(EncryptionContext context)
{
    context.security.encryption.revision.requireAtLeast(StandardSecurityHandlerRevision.R3,
            "Algorithm 5 requires a security handler of revision 3 or greater");
    digest.reset();
    digest.update(ENCRYPT_PADDING);
    byte[] encrypted = engine.encryptBytes(
            Arrays.copyOf(digest.digest(context.documentId()), 16), context.key());
    byte[] iterationKey = new byte[context.key().length];
    for (int i = 1; i < 20; i++)
    {
        iterationKey = Arrays.copyOf(context.key(), context.key().length);
        for (int j = 0; j < iterationKey.length; j++)
        {
            iterationKey[j] = (byte) (iterationKey[j] ^ (byte) i);
        }
        encrypted = engine.encryptBytes(encrypted, iterationKey);
    }
    return Arrays.concatenate(Arrays.copyOf(encrypted, 16), Arrays.copyOf(ENCRYPT_PADDING, 16));
}
 
Example 5
Source File: CallContractTxProcessor.java    From nuls-v2 with MIT License 4 votes vote down vote up
public Result onCommit(int chainId, ContractWrapperTransaction tx) {
    try {
        BlockHeader blockHeader = contractHelper.getBatchInfoCurrentBlockHeader(chainId);
        byte[] stateRoot = blockHeader.getStateRoot();
        long blockHeight = blockHeader.getHeight();
        ContractResult contractResult = tx.getContractResult();
        contractResult.setBlockHeight(blockHeight);

        // 保存代币交易
        ContractData callContractData = tx.getContractData();
        byte[] contractAddress = callContractData.getContractAddress();

        Result<ContractAddressInfoPo> contractAddressInfoPoResult = contractHelper.getContractAddressInfo(chainId, contractAddress);
        ContractAddressInfoPo contractAddressInfoPo = contractAddressInfoPoResult.getData();
        contractResult.setNrc20(contractAddressInfoPo.isNrc20());
        tx.setBlockHeight(blockHeight);
        // 获取合约当前状态
        ProgramStatus status = contractHelper.getContractStatus(chainId, stateRoot, contractAddress);
        boolean isTerminatedContract = ContractUtil.isTerminatedContract(status.ordinal());

        // 处理合约执行失败 - 没有transferEvent的情况, 直接从数据库中获取, 若是本地创建的交易,获取到修改为失败交易
        if (isTerminatedContract || !contractResult.isSuccess()) {
            if (contractAddressInfoPo != null && contractAddressInfoPo.isNrc20() && ContractUtil.isTransferMethod(callContractData.getMethodName())) {
                byte[] txHashBytes = tx.getHash().getBytes();
                byte[] infoKey = Arrays.concatenate(callContractData.getSender(), txHashBytes, new VarInt(0).encode());
                Result<ContractTokenTransferInfoPo> infoResult = contractTokenTransferStorageService.getTokenTransferInfo(chainId, infoKey);
                ContractTokenTransferInfoPo po = infoResult.getData();
                if (po != null) {
                    po.setStatus((byte) 2);
                    contractTokenTransferStorageService.saveTokenTransferInfo(chainId, infoKey, po);

                    // 刷新token余额
                    if (isTerminatedContract) {
                        // 终止的合约,回滚token余额
                        contractHelper.rollbackContractToken(chainId, po);
                        contractResult.setError(true);
                        contractResult.setErrorMessage("this contract has been terminated");
                    } else {

                        if (po.getFrom() != null) {
                            contractHelper.refreshTokenBalance(chainId, stateRoot, blockHeight, contractAddressInfoPo, AddressTool.getStringAddressByBytes(po.getFrom()), po.getContractAddress());
                        }
                        if (po.getTo() != null) {
                            contractHelper.refreshTokenBalance(chainId, stateRoot, blockHeight, contractAddressInfoPo, AddressTool.getStringAddressByBytes(po.getTo()), po.getContractAddress());
                        }
                    }
                }
            }
        }

        if (!isTerminatedContract) {
            // 处理合约事件
            contractHelper.dealNrc20Events(chainId, stateRoot, tx, contractResult, contractAddressInfoPo);
        }

        // 保存合约执行结果
        return contractService.saveContractExecuteResult(chainId, tx.getHash(), contractResult);
    } catch (Exception e) {
        Log.error("save call contract tx error.", e);
        return getFailed();
    }
}
 
Example 6
Source File: FakedHttpWrapper.java    From AgentX with Apache License 2.0 4 votes vote down vote up
private byte[] wrapInResponse(final byte[] bytes) {
    String header = HttpFaker.getRandomResponseHeader(Http.RESPONSE_200, true);
    header = header.replaceAll(Matcher.quoteReplacement("$"), String.valueOf(bytes.length));
    return Arrays.concatenate(header.getBytes(), bytes);
}
 
Example 7
Source File: DPAESXTSCipher.java    From InflatableDonkey with MIT License 4 votes vote down vote up
static byte[] tweakFunction(long tweakValue) {
    byte[] bs = Pack.longToLittleEndian(tweakValue);
    return Arrays.concatenate(bs, bs);
}