I've received the task to convert a CRC routine from C to java but I'm really struggling to get the CRC to match up with the documented example. Here's my documentation:
The algorithm used is CCITT-16 (0x1021 or
0x8408 reversed).The remainder is
initialized to all 1's (0xFFFF)
WORD crc16_lsb(BYTE *pData, WORD length)
{
BYTE i;
WORD data, crc;
crc = 0xFFFF;
if (length == 0)
return 0;
do
{
data = (WORD)0x00FF & *pData++;
crc = crc ^ data;
for (i = 8; i > 0; i--)
{
if (crc & 0x0001)
crc = (crc >> 1) ^ 0x8408;
else
crc >>= 1;
}
}
while (--length);
crc = ~crc;
return (crc);
}
As an example, for a Receiver Status Request message made up of the following bytes: 1B FF 41 00
The following 2 byte CRC would be calculated: 27 66
I started with the following javacode from here http://www.cs.princeton.edu/introcs/51data/CRC16CCITT.java.html
Any help would be greatly appreciated.
Without any closer look, you should know that Java has TWO shift-right operators: ">>" and ">>>". They differ in how the most significant bit is treated, and it may very well be that ">>>" is the most appropriate for CRC-calculations.
Related
I am using this hashing algorithm..
public long DEKHash(String str)
{
long hash = str.length();
for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
}
return hash;
}
I have modified it slightly so that it produces a larger number. But this number is larger than a long and then overflows and becomes negative. I am beginning Java and wanted to know if it is possible to get the real value?
I was looking into BigInteger but I am not having any luck with it.
Perhaps this will solve your problem. Use Long.unsignedCompare().
long a1 = 1;
long a2 = -1;
if (a2 < a1) {
// a2 is less which is what you would expect
System.out.println(a2);
}
if (Long.compareUnsigned(a1,a2) < 0) {
// here, a1 is less
System.out.println(a1);
}
prints
-1
1
Also, above you have
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
Did you know that >> extends the sign bit to the right. If you want to just right shift and not extend (i.e. fill with 0 bits, then do >>>)
I have to build a serial comunication app using python, the old app is only working on windows xp and was written in C. Now i have to switch to linux and i dont have a working driver. I started to code it myself. I got the protocol from the producing company of the serial device.
The serial device receives and sends data wich is ended by a CRC. I'm new to python and i dont have a solution for this, maybe someone can help me.
This is the CRC alghoritm:
ALGORITHM FOR CRC CALCULATION
The two CRC bytes are calculated according to the formula x^15 + 1. In the calculation are included all data bytes plus the byte for block end. Every byte passes through the calculation register from teh MSB to LSB.
Three working bytes are used - S1, S0 and TR
S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END)
S0 - Least significant byte from the CRC ( It is transmitted after S1)
TR - the current transmitted byte in the block.
The CRC is calculated as follows:
1. S1 and S0 are zeroed
2. TR is loaded with the current transmitted byte. The byte is transmitted.
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and TR are shifted one bit to the left.
3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END.
4. TR is loaded with 0 and point 3 is executed
5. TR is loaded with 0 and point 3 is executed
6. Byte S1 is transmitted
7. Byte S0 is transmitted
ALGORITHM FOR CRC CHECK ON RECEIVING
Three working bytes are used S1, S0 and RC
S1 - Most significant byte from the CRC ( it is received immediately after END)
S0 - Least significant byte from the CRC ( transmitted after S1)
RC - the current received byte in the block ( beginning from the first byte after BEG and ending 2 bytes after END).
The CRC is obtained as follows:
1. S1 and S0 are zeroed
2. RC is loaded with the current received byte
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and RC are shifted 8 times to the left
3.2. if the MSB of S1 is 1 then MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including 2 bytes after END.
S1 and S0 must be 0.
If there is someone wich can show me how to do it i'll be very gratefull.Thank you all.
EDIT 1:
I managed to get the same CRC procedure made by someone, but its made in java, i'm not that good with java. Maybe you can guide me into converting it in python. This is the code:
public class Crc {
public static final String CRC_NAME = "CRC-16-ECR";
private static final int POLYNOMIAL = 32769;
public static final int WIDTH = 16;
public static final int TOPBIT = 32768;
short CRC;
short[] crcTable = new short[256];
public Crc() {
this.crcInit();
}
private void crcInit() {
for(int dividend = 0; dividend < 256; ++dividend) {
int remainder = dividend << 8;
for(byte bit = 8; bit > 0; --bit) {
if((remainder & '耀') != 0) {
remainder = (remainder << 1 ^ '老') & '\uffff';
} else {
remainder = remainder << 1 & '\uffff';
}
}
this.crcTable[dividend] = (short)remainder;
}
}
public short crcFast(byte[] message, int nBytes) {
int remainder = 0;
for(int oneByte = 0; oneByte < nBytes; ++oneByte) {
int data = (message[oneByte] ^ remainder >> 8) & 255;
remainder = this.crcTable[data] ^ remainder << 8;
}
return (short)remainder;
}
}
#!/usr/bin/python
import sys
crc = 0
while True:
ch = sys.stdin.read(1)
if not ch:
break
crc ^= ord(ch) << 8
for _ in range(8):
crc = crc << 1 if (crc & 0x8000) == 0 else (crc << 1) ^ 0x8001
crc &= 0xffff
print(format(crc, '04x'))
What is the best method to create a hash of String, if the hash may not have more than 4 characters, and those 4 characters may only be lowercase letters or digits?
The strings I want to hash have 1-255 characters.
I know that it's probably impossible to create as 4-char hash without collision. But it would be sufficient if I'd have a good hash where possible collisions are minimized.
What I tried is the CRC16CCITT from here:
http://introcs.cs.princeton.edu/java/61data/CRC16CCITT.java
public class CRC16CCITT {
public static void main(String[] args) {
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
// byte[] testBytes = "123456789".getBytes("ASCII");
byte[] bytes = args[0].getBytes();
for (byte b : bytes) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
StdOut.println("CRC16-CCITT = " + Integer.toHexString(crc));
}
}
But this gives too many collision. Are there better algorithms?
You are mistaking "hexadecimal digits" for "characters":
int crc = 0xFFFF; // initial value
That's only 2 bytes (0xFF is just 1 byte). For a CRC of 4 ANSI characters, you need 4 bytes (0xFFFFFFFF).
You'll have to adapt the rest of the code to work with double the legth, please comment if you don't know how to do that.
PS: You could do it with less than 4 bytes, but that would complicate things more than necessary.
I am newbie applets and i used from this link: working with Java Card
Wallet for creating an Wallet project.
I before could credit card amount by this command : 80 30 00 00 01 1A 00.
I now want add '5000' to the present amount. As you know 5000 in hex equals
with '1388' that is 2 byte. So i must send 2 byte data 13 and 88 to the card.
I create bellow command and sent it to card but i get '67 00 Wrong lenght' as
response.
80 30 00 00 02 13 88 00
How can i credit or debit more than 1 byte to/from card?
You'll have to change the code of the Applet you're pointing to of course:
if ((numBytes != 1) || (byteRead != 1)) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); // constant with value 0x6700
}
So you must make sure that it allows for 2 bytes to be send, then you can use the Util.getShort method to convert to the bytes to a 16 bit signed value (using big endian two complement notation, as usual).
Replace the creadit() method, with this one. But remember that you must use two byte value for crediting you walled henceforth. (even for values less than 255 or 0xFF. i.e. you must use 0x00FF to debit you wallet with 255$ )
private void credit(APDU apdu) {
// access authentication
if (!pin.isValidated()) {
ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
}
byte[] buffer = apdu.getBuffer();
// Lc byte denotes the number of bytes in the
// data field of the command APDU
byte numBytes = buffer[ISO7816.OFFSET_LC];
// indicate that this APDU has incoming data
// and receive data starting from the offset
// ISO7816.OFFSET_CDATA following the 5 header
// bytes.
byte byteRead = (byte) (apdu.setIncomingAndReceive());
// it is an error if the number of data bytes
// read does not match the number in Lc byte
if ((numBytes != 2) || (byteRead != 2)) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
// get the creditBytes
byte[] creditBytes = new byte[2];
creditBytes[0]=buffer[ISO7816.OFFSET_CDATA];
creditBytes[1]=buffer[ISO7816.OFFSET_CDATA+1];
// convert 2 byte of creatBytes to a single short value.
short creditAmount = Util.getShort(creditBytes,(short)0);
// check the credit amount
if ((creditAmount > MAX_TRANSACTION_AMOUNT) || (creditAmount < 0)) {
ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
}
// check the new balance
if ((short) (balance + creditAmount) > MAX_BALANCE) {
ISOException.throwIt(SW_EXCEED_MAXIMUM_BALANCE);
}
// credit the amount
balance = (short) (balance + creditAmount);
}
I propose using BCD addition and BCD subtraction, as follow:
Each byte represent two BCD, e.g. 0x99 represent 99 instead of 153.
All data included in the addition and subtraction shall have the same length, e.g. 6 bytes will represents 12 digits. This should cover most cases, but if you need more, simply change your constant.
Your applet performs loop through the bytes to do the addition or subtraction. Encode and decode operation from BCD to the value and vice versa are needed before and after the operation.
Here is sample for the implementation. It is not tested yet, but should give you idea of how it works:
public class BCD {
public static final short NUMBER_OF_BYTES = 6;
static void add(byte[] augend, byte[] addend, byte[] result) {
byte carry = 0;
short temp = 0;
for (short i = (short) (NUMBER_OF_BYTES - 1); i >= 0; i--) {
temp = (short) (decode(augend[i]) + decode(addend[i]) + carry);
carry = (byte) ((temp > 100) ? 1 : 0);
result[i] = encode((byte) temp);
}
if (carry == 1) {
// TODO: result more than maximum
// you can set all digits to 9 or throw exception
}
}
static void subtract(byte[] minuend, byte[] subtrahend, byte[] result) {
byte borrow = 0;
short temp = 0;
for (short i = (short) (NUMBER_OF_BYTES - 1); i >= 0; i--) {
temp = (short) (100 + decode(minuend[i]) - decode(subtrahend[i]) - borrow);
borrow = (byte) ((temp < 100) ? 1 : 0);
result[i] = encode((byte) temp);
}
if (borrow == 1) {
// TODO: subtrahend > minuend,
// you can set all digits to 0 or throw exception
}
}
static byte encode(byte value) {
value %= 100; // only convert two digits, ignore borrow/carry
return (byte) (((value / 10) << 4) | (value % 10));
}
static byte decode(byte bcdByte) {
byte highNibble = (byte) ((bcdByte >> 4) & 0x0F);
byte lowNibble = (byte) (bcdByte & 0x0F);
if ((highNibble > 9) || (lowNibble > 9)) {
// found 'A' to 'F' character which should be invalid
// you can change this line, e.g. throwing exception
return 0;
}
return (byte) ((highNibble * 10) + lowNibble);
}
}
I have a Java CRC16 function like below and I need it to return a 4 length alphanumeric result. But sometimes it returns only a 3 length alphanumeric. Why? I am cross comparing this CRC with a C application that calculates the CRC and usually it is the same CRC but from time to time the JAVA returns a 3 character result which raises an exception. Why?
int crrc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
String str = "0009";
byte []by = x; // x is the byte array from args
for (byte b : by) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crrc >> 15 & 1) == 1);
crrc <<= 1;
if (c15 ^ bit) crrc ^= polynomial;
}
}
crrc &= 0xffff;
System.out.println(crrc);
System.out.println("CRC16-CCITT = " + Integer.toHexString(crrc) + " " + Integer.toHexString(crrc).length());
You're receiving ArrayIndexOutOfBoundException probably, so I think the hex value might have zero in its most significant nibble. Try to find out those values(x) and check whether (x >>> 12) == 0 or (x & 0xf000) == 0. If this returns true, you can pad your string with necessary number of 0s from the left.
One possible way: String.format("%04d", crrc)