Skip to content

Instantly share code, notes, and snippets.

@alandipert
Forked from anonymous/char.c
Created January 31, 2010 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/291265 to your computer and use it in GitHub Desktop.
Save alandipert/291265 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define ID_SIZE 6
// this is the array of authorized key strings that
// reader input is checked against
char *authorized_ids[] = {
"000013579EF3",
"\0"
};
// turn string input into 6 byte array, and reverse
void str2bytes(char *input, unsigned char *into) {
int i,q;
char buf[3]; buf[2] = '\0';
for(i=ID_SIZE*2-1,q=0; i>0; i-=2,q++) {
buf[0] = input[i-1];
buf[1] = input[i];
into[q] = strtol(buf,0,16);
}
}
// compare a known id's bytes with those from the reader
int matches_reader(unsigned char *id, unsigned char *reader) {
int i;
for(i = 0; i < ID_SIZE; i++) {
if(id[i] != reader[i+1])
return 0;
}
return 1;
}
// check input from reader against authorized IDs
int is_authorized(unsigned char *input) {
int i = 0;
char *checkstr;
unsigned char checkbytes[ID_SIZE];
while(*(checkstr = authorized_ids[i++]) != '\0') {
str2bytes(checkstr, checkbytes);
if(matches_reader(checkbytes, input))
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
unsigned char from_reader[] = {0x01, 0xF3, 0x9E, 0x57, 0x13, 0x00, 0x00, 0xF3};
if(is_authorized(from_reader)) {
printf("Authorized\n");
} else {
printf("Not Authorized\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment