Package io.nodyn.crypto

Source Code of io.nodyn.crypto.BigIntegerUtils

package io.nodyn.crypto;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.math.BigInteger;

/**
* @author Bob McWhirter
*/
public class BigIntegerUtils {

    public static ByteBuf toBuf(BigInteger i) {
        byte[] array = i.toByteArray();
        int byteLen = (int) Math.ceil( i.bitLength() / 8.0  );
        ByteBuf buf = Unpooled.buffer( byteLen );
        buf.writeBytes( array, array.length - byteLen, byteLen );
        return buf;
    }

    public static BigInteger fromBuf(ByteBuf buf) {
        byte[] array = new byte[ buf.readableBytes() + 1 ];
        array[0] = 0;
        buf.getBytes(0, array, 1, buf.readableBytes() );

        return new BigInteger( array );
    }
}
TOP

Related Classes of io.nodyn.crypto.BigIntegerUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.