Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SHA
- //#include <sha256.h>
- //#include <sha256_config.h>
- #include <sha1.h>
- #include <printf.h>
- #include <sha1_config.h>
- #include <TimeLib.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_PN532.h>
- // Connection
- byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0X02 };
- IPAddress ip(192, 168, 216, 50);
- //IPAddress timeServer(64, 99, 80, 121);
- IPAddress timeServer(202, 90, 132, 242);
- const int timeZone = 8;
- EthernetClient client;
- EthernetUDP Udp;
- unsigned int localPort = 8888;
- char server[] = "192.168.216.51";
- // If using the breakout with SPI, define the pins for SPI communication.
- #define PN532_IRQ (2)
- #define PN532_RESET (3)
- Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
- #if defined(ARDUINO_ARCH_SAMD)
- // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
- // also change #define in Adafruit_PN532.cpp library file
- #define Serial SerialUSB
- #endif
- void setup(void) {
- Serial.begin(9600);
- Serial.println("Hello!");
- nfc.begin();
- // NTP
- Ethernet.begin(mac, ip);
- if(Ethernet.begin(mac)==1){
- Serial.println("[+] Successfully connected");
- }
- Serial.println("waiting for sync");
- Udp.begin(localPort);
- setSyncInterval(43200); // Once every 30 minutes to sync
- //setSyncProvider(getNtpTime);
- setTime(getNtpTime());
- uint32_t versiondata = nfc.getFirmwareVersion();
- if (! versiondata) {
- Serial.print("Didn't find PN53x board");
- while (1); // halt
- }
- // Got ok data, print it out!
- Serial.print("Found chip PN5");
- Serial.println((versiondata>>24) & 0xFF, HEX);
- Serial.print("Firmware ver. ");
- Serial.print((versiondata>>16) & 0xFF, DEC);
- Serial.print('.');
- Serial.println((versiondata>>8) & 0xFF, DEC);
- // configure board to read RFID tags
- nfc.SAMConfig();
- Serial.println("Waiting for an ISO14443A Card ...");
- }
- time_t prevDisplay = 0; // when the digital clock was displayed
- void loop(void) {
- uint8_t success;
- uint8_t uid[] = {
- 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
- uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
- success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 10000);
- // NTP LOOP
- if (success) {
- String time = urlencode(digitalClockDisplay());
- /*Serial.println("Found an ISO14443A card");
- Serial.print(" UID Length: ");
- Serial.print(uidLength, DEC);
- Serial.println(" bytes");
- Serial.print(" UID Value: ");
- nfc.PrintHex(uid, uidLength);*/
- uint32_t cardid = uid[0];
- if (uidLength == 4)
- {
- // We probably have a Mifare Classic card ...
- //uint32_t cardid = uid[0]; M
- cardid <<= 8;
- cardid |= uid[1];
- cardid <<= 8;
- cardid |= uid[2];
- cardid <<= 8;
- cardid |= uid[3];
- //Serial.print("Seems to be a Mifare Classic card #");
- //Serial.println(cardid);
- //delay(2000);
- }
- //abcdefghijklmopqrstuvwxyz
- // Web Request
- if(client.connect(server,80)){
- Serial.println("t="+time);
- Serial.println("s="+(String)cardid);
- Sha1.init();
- Sha1.print((String)cardid+"abc");
- String hashed = GetHash(Sha1.result());
- String request = "GET /index.php?t="+time+"&s="+(String)cardid+"&c="+hashed+" HTTP/1.1";
- Serial.println(request);
- client.println("Host: 192.168.216.51");
- client.println("Connection: close");
- client.println();
- }
- Serial.println("");
- while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 100)){
- }
- }
- else
- {
- }
- while (client.available()) {
- char c = client.read();
- if(c == '\n') Serial.println();
- Serial.print(c);
- }
- resetEthernet(); // Only reset the ethernet settings when we are able to print request
- }
- //Crypto
- String GetHash(uint8_t* hash) {
- char tmp[16];
- String sHash256 = "";
- int i;
- for (i=0; i<20; i++) {
- sprintf(tmp, "%.2x",hash[i]);
- sHash256.concat(tmp);
- }
- //Serial.println(sHash256);
- return sHash256;
- }
- void resetEthernet(){
- client.stop();
- Ethernet.begin(mac, ip);
- }
- /*-------- NTP code ----------*/
- const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
- byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
- time_t getNtpTime()
- {
- while (Udp.parsePacket() > 0) ; // discard any previously received packets
- Serial.println("Transmit NTP Request");
- sendNTPpacket(timeServer);
- uint32_t beginWait = millis();
- while (millis() - beginWait < 1500) {
- int size = Udp.parsePacket();
- if (size >= NTP_PACKET_SIZE) {
- Serial.println("Receive NTP Response");
- Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
- unsigned long secsSince1900;
- // convert four bytes starting at location 40 to a long integer
- secsSince1900 = (unsigned long)packetBuffer[40] << 24;
- secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
- secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
- secsSince1900 |= (unsigned long)packetBuffer[43];
- return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
- }
- }
- Serial.println("No NTP Response :-(");
- return 0; // return 0 if unable to get the time
- }
- // send an NTP request to the time server at the given address
- void sendNTPpacket(IPAddress &address)
- {
- // set all bytes in the buffer to 0
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- // Initialize values needed to form NTP request
- // (see URL above for details on the packets)
- packetBuffer[0] = 0b11100011; // LI, Version, Mode
- packetBuffer[1] = 0; // Stratum, or type of clock
- packetBuffer[2] = 6; // Polling Interval
- packetBuffer[3] = 0xEC; // Peer Clock Precision
- // 8 bytes of zero for Root Delay & Root Dispersion
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- // all NTP fields have been given values, now
- // you can send a packet requesting a timestamp:
- Udp.beginPacket(address, 123); //NTP requests are to port 123
- Udp.write(packetBuffer, NTP_PACKET_SIZE);
- Udp.endPacket();
- }
- String digitalClockDisplay(){
- String t;
- t += (String)year()+ "-" + (String)printDigits(month()) + "-" + (String)printDigits(day());
- t += " ";
- t += printHour(hour()) + ":" + printDigits(minute()) + ":" + printDigits(second());
- return t;
- }
- String printDigits(int digits){
- if(digits < 10)
- return "0"+(String)digits;
- return (String)digits;
- }
- String printHour(int digits){
- unsigned int iHour;
- if(digits > 12 ){
- iHour = digits - 12;
- return "0"+(String)iHour;
- }
- if(digits < 10){
- return "0"+String(digits);
- }
- return (String)digits;
- }
- String urldecode(String str)
- {
- String encodedString="";
- char c;
- char code0;
- char code1;
- for (int i =0; i < str.length(); i++){
- c=str.charAt(i);
- if (c == '+'){
- encodedString+=' ';
- }else if (c == '%') {
- i++;
- code0=str.charAt(i);
- i++;
- code1=str.charAt(i);
- c = (h2int(code0) << 4) | h2int(code1);
- encodedString+=c;
- } else{
- encodedString+=c;
- }
- //yield();
- }
- return encodedString;
- }
- String urlencode(String str)
- {
- String encodedString="";
- char c;
- char code0;
- char code1;
- char code2;
- for (int i =0; i < str.length(); i++){
- c=str.charAt(i);
- if (c == ' '){
- encodedString+= '+';
- } else if (isalnum(c)){
- encodedString+=c;
- } else{
- code1=(c & 0xf)+'0';
- if ((c & 0xf) >9){
- code1=(c & 0xf) - 10 + 'A';
- }
- c=(c>>4)&0xf;
- code0=c+'0';
- if (c > 9){
- code0=c - 10 + 'A';
- }
- code2='\0';
- encodedString+='%';
- encodedString+=code0;
- encodedString+=code1;
- //encodedString+=code2;
- }
- //yield();
- }
- return encodedString;
- }
- unsigned char h2int(char c)
- {
- if (c >= '0' && c <='9'){
- return((unsigned char)c - '0');
- }
- if (c >= 'a' && c <='f'){
- return((unsigned char)c - 'a' + 10);
- }
- if (c >= 'A' && c <='F'){
- return((unsigned char)c - 'A' + 10);
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement