Example usage for java.math BigInteger toString

List of usage examples for java.math BigInteger toString

Introduction

In this page you can find the example usage for java.math BigInteger toString.

Prototype

public String toString() 

Source Link

Document

Returns the decimal String representation of this BigInteger.

Usage

From source file:jp.terasoluna.fw.validation.ValidationUtil.java

/**
 * ??????????/*from  ww  w  .  j av  a2 s  .  com*/
 * <br>
 * ??????????????
 * <ul>
 * <li>???
 * <ol>
 * <li><code>isAccordedInteger</code>?<code>true</code>???
 * ????<code>integerLength</code>??
 * ????????
 *
 * <li><code>isAccordedInteger</code>?<code>false</code>???
 * ????<code>integerLength</code>?????
 * ??
 * </ol>
 *
 * <li>????
 * <ol>
 * <li><code>isAccordedScale</code>?<code>true</code>???
 * ?????<code>scaleLength</code>??
 * ????????
 *
 * <li><code>isAccordedScale</code>?<code>true</code>???
 * ?????<code>scaleLength</code>?????
 * ??
 * </ol>
 * </ul>
 *
 * @param value 
 * @param integerLength ??
 * @param isAccordedInteger
 *           ??????
 *           <code>true</code>?
 *           ??????
 *           <code>false</code>?
 * @param scaleLength ???
 * @param isAccordedScale
 *           ???????
 *           <code>true</code>?
 *           ???????
 *           <code>false</code>?
 *
 * @return
 *            ????????
 *            <code>true</code>?
 *            ?????<code>false</code>?
 */
public static boolean isNumber(BigDecimal value, int integerLength, boolean isAccordedInteger, int scaleLength,
        boolean isAccordedScale) {

    // ?null??true?
    if (value == null) {
        return true;
    }

    // ??
    // ??
    BigInteger bi = value.toBigInteger().abs();
    // ?
    int length = bi.toString().length();
    if (!checkNumberFigures(length, integerLength, isAccordedInteger)) {
        return false;
    }

    // ???
    int scale = value.scale();
    if (!checkNumberFigures(scale, scaleLength, isAccordedScale)) {
        return false;
    }

    return true;
}

From source file:com.aaasec.sigserv.csspserver.SpServlet.java

private static SpSession getSession(HttpServletRequest request, HttpServletResponse response) {
    BigInteger sessionID = new BigInteger(32, new Random(System.currentTimeMillis()));
    Cookie[] cookies = request.getCookies();
    try {/*from  ww  w  . j a v a2 s  .  c o  m*/
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("SigSpSession")) {
                sessionID = new BigInteger(cookie.getValue());
            }
        }
    } catch (Exception ex) {
    }
    response.addCookie(new Cookie("SigSpSession", sessionID.toString()));
    return getSessionFromID(sessionID);
}

From source file:com.aegiswallet.utils.WalletUtils.java

public static List<ECKey> restoreWalletFromBackupFile(String fileName, String passOrNFC, Wallet wallet,
        boolean shouldAddKeys) {
    final List<ECKey> keys = new LinkedList<ECKey>();
    boolean nfcEncrypted;

    try {/*from w ww . j  av a2s . c o  m*/

        if (Constants.WALLET_BACKUP_DIRECTORY.exists() && Constants.WALLET_BACKUP_DIRECTORY.isDirectory()) {

            File file = new File(Constants.WALLET_BACKUP_DIRECTORY, fileName);

            FileInputStream fileInputStream = new FileInputStream(file);
            final BufferedReader in = new BufferedReader(
                    new InputStreamReader(fileInputStream, Constants.UTF_8));

            String x1 = null;
            String x2Encrypted = null;
            String x2Decrypted = null;
            String secretString = null;

            while (true) {
                final String line = in.readLine();
                if (line == null)
                    break; // eof

                if (line.startsWith("# "))
                    continue;

                if (line.trim().isEmpty())
                    continue;

                if (line.startsWith("#1:")) {
                    String[] splitStr = line.split(":");
                    x1 = splitStr[1];
                    continue;
                }

                if (line.startsWith("#X2:")) {

                    String[] splitStr = line.split(":");
                    String x2Base64 = splitStr[1];

                    x2Encrypted = new String(Base64.decode(x2Base64.getBytes(), Base64.NO_WRAP));
                    x2Decrypted = WalletUtils.decryptString(x2Encrypted, passOrNFC);
                    x2Decrypted = x2Decrypted.split(":")[1];
                    BigInteger secret = WalletUtils.generateSecretFromStrings("1:" + x1, "2:" + x2Decrypted,
                            null);
                    secretString = WalletUtils.convertToSha256(secret.toString());

                    continue;
                }

                if (line.startsWith("#ENCTYPE:NFC")) {
                    x2Decrypted = passOrNFC;
                    BigInteger secret = WalletUtils.generateSecretFromStrings("1:" + x1, x2Decrypted, null);
                    secretString = WalletUtils.convertToSha256(secret.toString());
                    continue;
                }

                if (line.startsWith("#ENCTYPE:PASSWORD"))
                    continue;

                String encryptedKey = new String(Base64.decode(line.getBytes(), Base64.NO_WRAP));
                String plainKey = WalletUtils.decryptString(encryptedKey, secretString);
                ECKey key = new DumpedPrivateKey(Constants.NETWORK_PARAMETERS, plainKey).getKey();

                if (!wallet.hasKey(key))
                    keys.add(key);
            }

            //Only add keys if the parameter says so. This is because the wallet may be still encrypted.
            //We dont want to add keys to an encrypted wallet. That's bad.
            if (shouldAddKeys)
                wallet.addKeys(keys);

        }

    } catch (final AddressFormatException x) {
        Log.e(TAG, "exception caught: " + x.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "exception caught: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "exception caught: " + e.getMessage());
    }

    return keys;

}

From source file:com.aegiswallet.utils.WalletUtils.java

public static void writeEncryptedKeys(@Nonnull final Writer out, @Nonnull final List<ECKey> keys,
        SharedPreferences prefs, String passOrNFC) throws IOException {

    boolean nfcEnabled = prefs.contains(Constants.SHAMIR_ENCRYPTED_KEY) ? false : true;

    String x1 = prefs.getString(Constants.SHAMIR_LOCAL_KEY, null);
    String x2 = null;//w  w  w.j  a  va2s .  co m
    String encodedEncryptedX2 = null;

    if (!nfcEnabled) {
        x2 = prefs.getString(Constants.SHAMIR_ENCRYPTED_KEY, null);
        String encryptedX2 = encryptString(x2, passOrNFC);
        encodedEncryptedX2 = Base64.encodeToString(encryptedX2.getBytes("UTF-8"), Base64.NO_WRAP);
    }

    out.write("# PRIVATE KEYS ARE ENCRYPTED WITH SHAMIR SECRET SHARING\n");
    out.write("# TO DECRYPT - Import this backup and provide your password or NFC token\n");
    out.write("# If password/NFC token are lost, contact Bitcoin Security Project. We may be able to help.\n");
    out.write("#" + x1);
    out.write("\n");

    if (!nfcEnabled && encodedEncryptedX2 != null) {
        out.write("#X2:" + encodedEncryptedX2);
        out.write("\n");
        out.write("#ENCTYPE:PASSWORD");
    }
    //Means NFC is enabled and we're using that for encryption
    else if (nfcEnabled) {
        out.write("#ENCTYPE:NFC");
    }

    out.write("\n");

    BigInteger mainKey = null;
    if (nfcEnabled) {
        mainKey = generateSecretFromStrings(x1, passOrNFC, null);
    } else if (x2 != null) {
        mainKey = generateSecretFromStrings(x1, x2, null);
    }

    String mainKeyHash = convertToSha256(mainKey.toString());

    for (final ECKey key : keys) {
        String encodedKey = key.getPrivateKeyEncoded(Constants.NETWORK_PARAMETERS).toString();
        String encryptedKey = encryptString(encodedKey, mainKeyHash);

        out.write(Base64.encodeToString(encryptedKey.getBytes(), Base64.NO_WRAP));
        out.write('\n');
    }
}

From source file:com.aegiswallet.utils.WalletUtils.java

public static String getWalletCurrencyValue(Context context, SharedPreferences prefs, BigInteger balance) {
    String result = "";

    File file = context.getApplicationContext().getFileStreamPath(Constants.BLOCKCHAIN_CURRENCY_FILE_NAME);
    if (file.exists()) {
        JSONObject jsonObject = BasicUtils.parseJSONData(context, Constants.BLOCKCHAIN_CURRENCY_FILE_NAME);
        try {//from w  w  w  . ja va 2s  . com

            String balanceInBTC = balance.toString();

            if (balance.longValue() > 0)
                balanceInBTC = BasicUtils.formatValue(balance, Constants.BTC_MAX_PRECISION, 0);
            BigDecimal formattedBalance = new BigDecimal(balanceInBTC);

            if (jsonObject != null) {

                JSONObject newObject = jsonObject
                        .getJSONObject(prefs.getString(Constants.CURRENCY_PREF_KEY, null));
                Double doubleVal = newObject.getDouble("last");
                BigDecimal decimal = BigDecimal.valueOf(doubleVal);

                result = newObject.getString("symbol")
                        + decimal.multiply(formattedBalance).setScale(2, RoundingMode.HALF_EVEN).toString();
            }
        } catch (JSONException e) {
            Log.e("Wallet Utils", "JSON Exception " + e.getMessage());
        }
    }

    return result;
}

From source file:co.rsk.core.NetworkStateExporter.java

private ObjectNode createAccountNode(ObjectNode mainNode, byte[] address, Repository frozenRepository) {
    ObjectNode accountNode = mainNode.objectNode();
    AccountState accountState = frozenRepository.getAccountState(address);
    BigInteger balance = accountState.getBalance();
    accountNode.put("balance", balance.toString());
    BigInteger nonce = accountState.getNonce();
    accountNode.put("nonce", nonce.toString());
    ContractDetails contractDetails = frozenRepository.getContractDetails(address);
    String addWrapper = Hex.toHexString(address);
    if (!contractDetails.isNullObject() && !StringUtils.equals(PrecompiledContracts.REMASC_ADDR, addWrapper)) {
        accountNode.set("contract", createContractNode(contractDetails, accountNode));
    }//from   w w  w  .  ja v  a2s .  c  o m
    return accountNode;
}

From source file:org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils.java

public static String getNodeIdFromDpnId(BigInteger dpnId) {
    return MDSALUtil.NODE_PREFIX + MDSALUtil.SEPARATOR + dpnId.toString();
}

From source file:com.diversityarrays.dalclient.DalUtil.java

/**
 * Generate a 64-bit random number.//ww w  .  j a  v  a  2s .c o m
 * @return the number as a String
 */
static public String createRandomNumberString() {
    SecureRandom random = new SecureRandom();

    byte[] sixtyFourBits = new byte[8];
    random.nextBytes(sixtyFourBits);
    // Let's be positive about this :-)
    sixtyFourBits[0] = (byte) (0x7f & sixtyFourBits[0]);

    BigInteger bigint = new BigInteger(sixtyFourBits);
    return bigint.toString();
}

From source file:cherry.foundation.crypto.SecureBigIntegerEncoderTest.java

@Test
public void testEncodeDecode() throws Exception {
    SecureBigIntegerEncoder encoder = createSecureBigIntegerEncoder();
    for (int i = 0; i < 100; i++) {
        BigInteger plain = BigInteger.valueOf(random.nextLong());
        String crypto = encoder.encode(plain);
        assertThat(crypto, is(not(plain.toString())));
        assertThat(encoder.decode(crypto), is(plain));
    }/*from   w w  w.j  ava2  s. c  om*/
}

From source file:be.fedict.trust.service.entity.RevokedCertificateEntity.java

public RevokedCertificateEntity(String issuer, BigInteger serialNumber, Date revocationDate,
        BigInteger crlNumber) {/*  www  .j a va 2 s.  co  m*/
    this.pk = new RevokedCertificatePK(issuer, serialNumber.toString());
    this.revocationDate = revocationDate;
    this.crlNumber = crlNumber;
}