|
BigIntegerUtil |
|
1 /* $RCSfile: BigIntegerUtil.java,v $ 2 * $Revision: 1.2 $ 3 * $Date: 2002/01/04 14:05:50 $ 4 * $Author: uwe $ 5 * $State: Exp $ 6 * 7 * Created on December 20, 2001 10:13 AM 8 * 9 * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de> 10 * 11 * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE- 12 * ServiceProvider is a library, written in JavaTM, that should be 13 * used in HBCI banking applications (clients and may be servers), 14 * to do cryptographic operations. 15 * 16 * The jhbci library is free software; you can redistribute it and/or 17 * modify it under the terms of the GNU Lesser General Public 18 * License as published by the Free Software Foundation; either 19 * version 2.1 of the License, or (at your option) any later version. 20 * 21 * The jhbci library is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 * Lesser General Public License for more details. 25 * 26 * You should have received a copy of the GNU Lesser General Public 27 * License along with this library; if not, write to the Free Software 28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 29 * 30 */ 31 32 package de.cscc.crypto.util; 33 34 import java.math.BigInteger; 35 36 /** 37 * BigIntegerUtil Class. 38 * 39 * <p>This is a wrapper class for frequentliy used methods that are missed in 40 * the BigInteger class. Because BigInteger is immutable we are not able to 41 * extend this class. 42 * 43 * @author <a href=mailto:uwe@cscc.de>Uwe Günther</a> 44 * 45 * @version $Revision: 1.2 $ 46 */ 47 public final class BigIntegerUtil { 48 49 /** This class can't be instance, because its constructor is private.*/ 50 private BigIntegerUtil() {} 51 52 /** 53 * Cuts a leading 0x00 byte that indicates that this number is non negative. 54 * 55 * <p>BigInterger#toByteArray returns a two's-complement representation 56 * of the BigInteger. This does not matter because we hav a non negative 57 * number (number >= 0). Two's Complement uses the leftmost bit (most 58 * significant bit) for the sign bit. Because BigInteger represents 59 * infinite numbers, there is no concrete sign bit. This isn't realy a 60 * problem for us, because positive numbers has no sign bit. But what, if 61 * a positive number where the leftmost bit in its leftmost byte is set? 62 * Is it a negative or a positive number? For these cases 63 * BigInteger#toByteArray adds a whole 0x00 byte add the left side to 64 * indicate this number is positive. This byte is bad for us, so we want to 65 * filter it out in this method. 66 * 67 * @param signature the signature that will be converted to an unsigned byte[]. 68 * @throws NullPointerException if signature is null. 69 * @throws IllegalArgumentException if signature is a negative number. 70 * @see java.math.BigInteger#toByteArray 71 */ 72 public static byte[] toUnsignedByteArray(BigInteger signature) { 73 74 if (signature == null) { 75 throw new NullPointerException("Parameter signature is null."); 76 } 77 if (signature.compareTo(BigInteger.ZERO) < 0) { 78 //signature < ZERO 79 throw new IllegalArgumentException( 80 "Parameter signature is negative. signature: " + signature); 81 } 82 83 //Fetch two's-complement representation of the signature (BigInteger) 84 byte[] twosComplement = signature.toByteArray(); 85 86 //If there is no leading Zero byte, we are right. 87 if (twosComplement[0] != 0x00) { 88 return twosComplement; 89 } 90 //We have a leading 0x00 byte, but twosComplement.length is only 1, that 91 //means our signature has the number "0". 92 if (twosComplement.length == 1) { 93 return twosComplement; 94 } 95 //If we are at this point, we should cut the leading 0x00 byte that 96 //is only an indicator that our number is not negative. 97 byte[] unsigned = new byte[twosComplement.length - 1]; 98 for (int i = 0; i < unsigned.length; i++) { 99 unsigned[i] = twosComplement[i + 1]; 100 } 101 //We return the new unsigned value. 102 return unsigned; 103 } 104 105 106 } 107
|
BigIntegerUtil |
|