Example usage for java.math BigInteger BigInteger

List of usage examples for java.math BigInteger BigInteger

Introduction

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

Prototype

private BigInteger(byte[] magnitude, int signum) 

Source Link

Document

This private constructor is for internal use and assumes that its arguments are correct.

Usage

From source file:com.saasovation.identityaccess.resource.AbstractResource.java

protected String userETag(User aUser) {

    int hashCode = aUser.hashCode() + aUser.person().hashCode();

    try {/* w w w .j a  v a2 s. com*/
        // change this algorithm as needed
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(Integer.toString(hashCode).getBytes("UTF-8"));
        BigInteger digestValue = new BigInteger(1, messageDigest.digest());
        String strongHash = digestValue.toString(16);

        return strongHash;

    } catch (Throwable t) {
        return Integer.toString(hashCode);
    }
}

From source file:fi.vm.kapa.identification.adapter.utils.AuthenticationHandlerUtils.java

public String createToken() {
    return new BigInteger(64, new SecureRandom()).toString(16);
}

From source file:io.kodokojo.service.redis.AbstractRedisStore.java

public AbstractRedisStore(Key key, String host, int port) {
    if (key == null) {
        throw new IllegalArgumentException("key must be defined.");
    }//from ww w.  j a  v  a 2s.  c om
    if (isBlank(host)) {
        throw new IllegalArgumentException("host must be defined.");
    }
    this.key = key;
    pool = createJedisPool(host, port);
    try {
        messageDigest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Unable to get instance of SHA-1 digest");
    }

    SecureRandom secureRandom = new SecureRandom();
    saltKey = new BigInteger(128, secureRandom).toString(10);
}

From source file:io.github.hebra.elasticsearch.beat.meterbeat.device.dlink.DSPW215.java

@Override
public String fetchData() {
    final String url = config.getBaseurl().concat("/my_cgi.cgi?")
            .concat(new BigInteger(130, secureRandom).toString(32));

    try {/*from ww  w.  ja v a 2s  .  co m*/
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout)
                .setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).build();

        HttpClient client = HttpClientBuilder.create().setConnectionTimeToLive(5, TimeUnit.SECONDS)
                .setConnectionReuseStrategy(new NoConnectionReuseStrategy()).build();
        HttpPost post = new HttpPost(url);

        post.setConfig(requestConfig);

        post.setHeader("User-Agent", USER_AGENT);
        post.setHeader("Accept-Language", "en-US,en;q=0.5");

        final List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair("request", "create_chklst"));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        final HttpResponse response = client.execute(post);

        final String content = IOUtils.toString(response.getEntity().getContent(), Charsets.US_ASCII);

        EntityUtils.consume(response.getEntity());

        return content.split("Meter Watt: ", 2)[1].trim();
    } catch (IOException ioEx) {
        log.error("Timeout when trying to read from {} on {}: {}", config.getName(), config.getBaseurl(),
                ioEx.getMessage());
    }

    return null;
}

From source file:com.nloko.android.Utils.java

public static String getMd5Hash(byte[] input) {
    if (input == null) {
        throw new IllegalArgumentException("input");
    }/*from   w ww.j  a v  a2  s. c o  m*/

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input);
        BigInteger number = new BigInteger(1, messageDigest);
        //String md5 = number.toString(16);
        StringBuffer md5 = new StringBuffer();
        md5.append(number.toString(16));

        while (md5.length() < 32) {
            //md5 = "0" + md5;
            md5.insert(0, "0");
        }

        return md5.toString();
    } catch (NoSuchAlgorithmException e) {
        Log.e("MD5", e.getMessage());
        return null;
    }
}

From source file:com.offbynull.peernetic.common.identification.Id.java

/**
 * Constructs a {@link Id} from byte arrays. It's assumed that the byte arrays contain unsigned values and are aligned to byte
 * boundaries (e.g. if you want to use the 6 bits instead of a whole byte (8 bits), make sure the 2 top-most bits are 0).
 * @param data id value// w  w w  .  j av  a  2 s. c o  m
 * @param limit limit value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code data > limit}
 */
public Id(byte[] data, byte[] limit) {
    this(new BigInteger(1, data), new BigInteger(1, limit));
}

From source file:com.github.ffremont.microservices.springboot.node.tasks.InstallJarTask.java

@Override
public void run(MicroServiceTask task) throws TaskException {
    Path msVersionFolder = helper.targetDirOf(task.getMs());
    Path checksumPath = Paths.get(msVersionFolder.toString(), InstallTask.CHECKSUM_FILE_NAME + ".txt");
    Path jarTarget = helper.targetJarOf(task.getMs());

    try {//w w  w  .j  av a2s. c  o  m
        if (task.getJar() == null) {
            throw new InvalidInstallationException("Jar indisponible dans l'objet MicroServiceTask");
        }

        Files.copy(task.getJar(), jarTarget);
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        try (DigestInputStream digestIs = new DigestInputStream(new FileInputStream(jarTarget.toFile()), md)) {
            byte[] buffer = new byte[10240]; // 10ko
            while (0 < digestIs.read(buffer)) {
            }

            digestIs.close();
        }
        byte[] checksumTarget = md.digest();

        if (!String.format("%032X", new BigInteger(1, checksumTarget))
                .equals(task.getMs().getSha1().toUpperCase())) {
            throw new InvalidInstallationException(
                    "Le checksum n'est pas valide suite  la copie : " + this.nodeBase);
        }

        Files.write(checksumPath, task.getMs().getSha1().getBytes());
        LOG.debug("Cration du fichier checksum.txt");
    } catch (IOException | NoSuchAlgorithmException ex) {
        throw new InvalidInstallationException("Impossible d'installer le jar", ex);
    }
}

From source file:JavaCloud.Utils.java

public static String calcHash(String password, String seed) {
    try {/*from  ww  w .j a  va 2 s. c o m*/
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();

        String tuple = password + seed;

        crypt.update(tuple.getBytes("UTF-8"));
        return new BigInteger(1, crypt.digest()).toString(16);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.nirima.jenkins.repo.build.MetadataChecksumRepositoryItem.java

protected String generateContent() {
    try {//from ww w. j  ava2  s .  c om
        MessageDigest md = MessageDigest.getInstance(algorithm.toUpperCase());
        byte[] digest = md.digest(IOUtils.toByteArray(item.getContent()));
        String hex = new BigInteger(1, digest).toString(16);

        // Need to prepend with 0s if not the correct length
        int requiredNumberOfCharacters = md.getDigestLength() * 2;
        while (hex.length() < requiredNumberOfCharacters) {
            hex = "0" + hex;
        }

        return hex;
    } catch (Exception nsae) {
        return "ERROR: " + nsae.getMessage();
    }
}

From source file:com.google.u2f.TestUtils.java

public static PrivateKey parsePrivateKey(String keyBytesHex) {
    try {/*  www .  j a  v  a 2  s . c  om*/
        KeyFactory fac = KeyFactory.getInstance("ECDSA");
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECParameterSpec curveSpec = new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(),
                curve.getH());
        ECPrivateKeySpec keySpec = new ECPrivateKeySpec(new BigInteger(keyBytesHex, 16), curveSpec);
        return fac.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
}