?? biginteger.java
字號:
package rsa;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import java.io.PrintStream;/** * A class implementing arbitrary length integers. * <p>Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/) * All rights reserved. */public class BigInteger extends BigNum{ public static final BigInteger zero = new BigInteger(); public static final BigInteger one = new BigInteger( 1 ); static { zero.zero(); } public BigInteger() { super(); } public BigInteger( int from ) { super(); assign( from ); } public BigInteger( BigInteger from ) { super(); copy( this, from ); } public BigInteger( String hexString ) { super(); fromString( hexString ); } public BigInteger( byte buffer[] ) { super(); fromByteArray( buffer ); } public int bitLength() { return bitLength(this); } public Object clone() { return new BigInteger( this ); } public void copy( Object src ) { copy( this, (BigInteger)src ); } public BigInteger assign( BigInteger n ) { copy( this, n ); return this; } public BigInteger assign( int n ) { assign( this, n ); return this; } public void fromString(String inHex) { int len=inHex.length(); if ( len == 0 ) { zero( this ); return; } int pos =0; byte buffer[] = new byte [( ( len + 1 ) / 2 )]; if ( ( len % 2 ) == 1 ) { buffer[0]=(byte)asciiToHex(inHex.charAt(0)); pos=1; len--; } for(int ptr = pos; len > 0; len -= 2 ) buffer[pos++] = (byte)( ( asciiToHex( inHex.charAt( ptr++ ) ) << 4 ) | ( asciiToHex( inHex.charAt( ptr++ ) ) ) ); fromBinary( buffer ); } public String toString() { byte buffer[] = toByteArray(); StringBuffer returnBuffer = new StringBuffer(); int pos = 0, len = buffer.length; // remove leading 0's. while ( ( pos < len ) && ( buffer[pos] == 0 ) ) pos++; if ( ( pos < len ) && ( ( ( buffer[pos] >>> 4 ) & 0x0F ) == 0 ) ) { returnBuffer.append( hexToAscii( buffer[0] & 0x0F ) ); pos++; } for (; pos<len; pos++) returnBuffer.append( hexToAscii( ( buffer[pos] >>> 4 ) & 0x0F ) ) .append( hexToAscii( buffer[pos] & 0x0F ) ); if ( returnBuffer.length() > 0 ) return returnBuffer.toString(); return "0"; } // protected void // finalize() // { // super.finalize(); // } public BigInteger modExp(BigInteger power, BigInteger modulo) { modExp( this, this, power, modulo ); return this; } public BigInteger inverseModN( BigInteger a, BigInteger n ) { inverseModN( this, a, n ); return this; } public BigInteger mod( BigInteger a, BigInteger b ) { mod( this, a, b ); return this; } public BigInteger mul( BigInteger a, BigInteger b ) { mul( this, a, b ); return this; } public BigInteger div( BigInteger a, BigInteger b ) { div( this, a, b ); return this; } public BigInteger add( BigInteger a, BigInteger b ) { add( this, a, b ); return this; } public BigInteger add( int a ) { add( this, a ); return this; } public BigInteger sub( BigInteger a, BigInteger b ) { sub( this, a, b ); return this; } public BigInteger gcd( BigNum a, BigNum b ) { gcd( this, a, b ); return this; } public int cmp( BigInteger a ) { return cmp( this, a ); } public boolean equals( Object o ) { if (!(o instanceof BigInteger)) return false; return (cmp( (BigInteger) o ) == 0); } public BigInteger inc() { inc(this); return this; } public BigInteger dec() { dec(this); return this; } public BigInteger shiftLeft(int n) { shiftLeft(this, this, (short)n); return this; } public BigInteger shiftRight(int n) { shiftRight(this, this, (short)n); return this; } public static BigInteger zero() { BigInteger r = new BigInteger(); zero( r ); return r; } /** * An internal function to create a buffer big enough in which * to store a number. * @param bitLen The number of <b>bits</b> in the number. * @return a buffer in which to store a number. */ protected static final byte[] newBuffer( int bitLen ) { return new byte[( bitLen + 7 ) / 8]; } /** * Convert a number into a byte array * @return a byte array */ public byte[] toByteArray() { byte buffer[] = newBuffer( bitLength() ); intoBinary( buffer ); return buffer; } public void fromByteArray( byte buf[] ) { fromBinary( buf ); } private static final int asciiToHex(char c) { if ( ( c >= 'a' ) && ( c <= 'f' ) ) return ( c - 'a' + 10 ); if ( ( c >= 'A' ) && ( c <= 'F' ) ) return ( c - 'A' + 10 ); if ( ( c >= '0' ) && ( c <= '9' ) ) return ( c - '0' ); throw new MathError("ascii to hex failed"); } private static char hexToAscii(int h) { if ( ( h >= 10 ) && ( h <= 15 ) ) return (char)( 'A' + ( h - 10 ) ); if ( ( h >= 0 ) && ( h <= 9 ) ) return (char)( '0' + h ); throw new MathError("hex to ascii failed"); } // // Test code // public static void main(String argv[]) { try { self_test(System.out, argv); } catch(Throwable t) { t.printStackTrace();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -