public static int verifyMessage(byte[] data, int len, bool verbose) { long calced_cksum = 0; if (len < MINIMUM_VERIFIABLE_MSG_LEN) { return(-1); // too small to be a verifiable msg } calced_cksum = VerifiableMessage.inet_cksum(data, len); if (verbose) { System.Console.WriteLine("Calculated cksum = " + calced_cksum); } if (calced_cksum == 0) { return(1); // success } for (int i = 0; i < 4; i++) { if (data[i + 2] != MAGIC_NUMBER[i]) { return(-1); // no magic number found, not a verifiable message - failed } } return(0); // magic number found, but bad checksum - failed }
public static byte[] constructVerifiableMessage(int len) { // bytes 0 and 1 are for the checksum // bytes 2 through 5 are for the 4 byte magic number // remaining bytes are generated randomly byte[] message = new byte[len]; long cksum; Array.Copy(MAGIC_NUMBER, 0, message, 2, 4); for (int i = 6; i < len; i++) { message[i] = (byte)(GENERATOR.Next() & 0xff); } cksum = VerifiableMessage.inet_cksum(message, len); message[0] = (byte)((cksum >> 8) & 0xff); message[1] = (byte)(cksum & 0xff); return(message); }