/*
 * [TestBigInteger.java]
 *
 * Summary: Demonstrate the basic use of BigInteger.
 *
 * Copyright: (c) 2006-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2006-02-28
 */
package com.mindprod.example;

import java.math.BigInteger;
import java.security.SecureRandom;

import static java.lang.System.*;

/**
 * Demonstrate the basic use of BigInteger.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2006-02-28
 * @since 2006-02-28
 */
@SuppressWarnings( { "UnusedAssignment" } )
public final class TestBigInteger
    {
    /**
     * Test BigInteger
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        BigInteger a = new BigInteger( "1234567890123456789012234" );
        BigInteger b = new BigInteger( "-12345" );
        BigInteger c = a.add( b );
        out.println( c );
        c = c.multiply( BigInteger.TEN );
        out.println( c );
        // valueOf for longs is faster than using the constructor
        c = c.divide( BigInteger.valueOf( 1024 ) );
        out.println( c );
        // generate a suitable value for private key encryption
        // 1024 bits long, probably prime.
        BigInteger p = new BigInteger( 1024
                /* size in bits */,
                100
                /* 1 in 100 chance not prime */,
                new SecureRandom() );
        // chaining operations
        BigInteger d = BigInteger.valueOf( 7 )
                .modInverse( p.subtract( BigInteger.ONE ) );
        out.println( d );
        // exporting to a byte array
        byte[] dbytes = d.toByteArray();
        }
    }