2017-01-16 1 views
-1

Je suis en train de créer un projet dans lequel je dois générer aléatoirement des nombres de hachage.Générer un hachage d'une chaîne formée de chiffres et de lettres dans Arduino

Le code doit fonctionner comme suit. Une fonction aléatoire génère un nombre aléatoire, ces nombres sont concaténés avec une chaîne pour générer le hachage de cette chaîne, c'est-à-dire que chaque fois qu'un nombre est généré, le hachage de ce nombre est également généré. Pour générer le hachage, j'utilise une bibliothèque appelée SpritzCipher.

Le problème avec ce code est qu'il génère le hachage du mot "random" au lieu du hachage de la concaténation de la valeur avec String (salt).

Voici le code que je l'ai fait:

#include <SpritzCipher.h> 

String salt = "fkllrnjnfd"; 
int randNumber; 
String valorConcat; 

void randomico() { 
    //Serial.begin(9600); 
    randomSeed(analogRead(0)); // Starts the list of random values 

    randNumber = random(100); // Draw a number from 0 to 99 

    valorConcat = String(randNumber) + salt; 

    //Serial.println(valorConcat); // Sends the value of randNumber to the serial 
    delay(500); // espera 500 ms 
} 


/* Data to input */ 

const byte testData[12] = {randomico}; 

/* Test vectors */ 
/* Data = 'testData' hash test vectors */ 
const byte testVector[32] = 
{ 0xff, 0x8c, 0xf2, 0x68, 0x09, 0x4c, 0x87, 0xb9, 
    0x5f, 0x74, 0xce, 0x6f, 0xee, 0x9d, 0x30, 0x03, 
    0xa5, 0xf9, 0xfe, 0x69, 0x44, 0x65, 0x3c, 0xd5, 
    0x0e, 0x66, 0xbf, 0x18, 0x9c, 0x63, 0xf6, 0x99 
}; 


void testFunc(const byte ExpectedOutput[32], const byte *data, byte dataLen) 
{ 
    byte hashLen = 32; /* 256-bit */ 
    byte digest[hashLen]; /* Output buffer */ 
    byte digest_2[hashLen]; /* Output buffer for chunk by chunk API */ 
    spritz_ctx hash_ctx; /* the CTX for chunk by chunk API */ 
    unsigned int i; 

    /* Print input */ 
    for (i = 0; i < dataLen; i++) { 
    Serial.write(data[i]); 
    } 
    Serial.println(); 

    spritz_hash_setup(&hash_ctx); 
    /* For easy test: code add a byte each time */ 
    for (i = 0; i < dataLen; i++) { 
    spritz_hash_update(&hash_ctx, data + i, 1); 
    } 
    spritz_hash_final(&hash_ctx, digest_2, hashLen); 

    spritz_hash(digest, hashLen, data, dataLen); 

    for (i = 0; i < sizeof(digest); i++) { 
    if (digest[i] < 0x10) { /* To print "0F" not "F" */ 
     Serial.write('0'); 
    } 
    Serial.print(digest[i], HEX); 
    } 

    /* Check the output */ 
    if (spritz_compare(digest, ExpectedOutput, sizeof(digest)) || spritz_compare(digest_2, ExpectedOutput, sizeof(digest_2))) { 
    /* If the output is wrong "Alert" */ 
    digitalWrite(LED_BUILTIN, HIGH); /* Turn pin LED_BUILTIN On (Most boards have this LED connected to digital pin 13) */ 
    Serial.println("\n** WARNING: Output != Test_Vector **"); 
    } 
    Serial.println(); 
} 

void setup() { 
    /* Initialize serial and wait for port to open */ 
    Serial.begin(9600); 
    while (!Serial) { 
    ; /* Wait for serial port to connect. Needed for Leonardo only */ 
    } 

    /* initialize digital pin LED_BUILTIN (Most boards have this LED connected to digital pin 13) as an output */ 
    pinMode(LED_BUILTIN, OUTPUT); 
    digitalWrite(LED_BUILTIN, LOW); 
} 

void loop() { 
    Serial.println("[Spritz spritz_hash*() test]\n"); 


    /* Data: arcfour */ 
    testFunc(testVector, testData, sizeof(testData)); 

    delay(5000); /* Wait 5s */ 
    Serial.println(); 
} 
+1

Vous Hashage 'testData', mais vous Concaténation dans' valorConcat'. Et 'const octet testData [12] = {randomico};' n'a pas de sens. Vous l'initialisez simplement comme l'adresse de la fonction 'randomico'. Le compilateur devrait vous avoir donné un avertissement. –

+1

De même, comment savez-vous que vous êtes en train de hacher la chaîne '" random "'? Votre code n'imprime aucune de ces chaînes. Et votre 'testVector' est un hachage de la chaîne' "arcfour" 'et ne correspondra à aucun des hachages pour vos chaînes salées aléatoires. Même si vous mettez 'arcfour' dans' testData', 'testVector' ne correspondra pas, car la chaîne' "arcfour \ 0 \ 0 \ 0 \ 0 \ 0" 'sera hachée. Vous devriez utiliser 'strlen' au lieu de' sizeof'. –

+0

Comment puis-je utiliser cet algorithme pour générer le hachage d'une chaîne générée par "void random()"? Savez-vous comment le faire? –

Répondre

0

Beaucoup de votre code de hachage est juste et exemple de deux options différentes de hachage un tableau d'octets et de le comparer à un hachage connu.

Puisque vous avez seulement besoin de calculer le hachage, vous devez seulement appeler spritz_hash. En ce qui concerne votre génération de chaîne, une méthode plus efficace consisterait à utiliser des fonctions C++ de bas niveau, car l'objet String utilise l'allocation de mémoire dynamique et can cause memory problems.

#include <SpritzCipher.h> 

#define hex_char(n) ((n) < 10 ? '0' + (n) : 'A' + ((n)-10)) 

const char salt[] = "fkllrnjnfd"; 
char string[13] = ""; 
byte hash[32]; // byte hash 
char hash_string[65]; // hex string hash 

void randomico(char *string) 
{ 
    int randNumber = random(100); // Draw a number from 0 to 99 
    itoa(randNumber, string, 10); // convert int into char array 
    strcat(string, salt); // concatenate salt 
} 

void bytes_to_hexstr(char *string, byte *bytes, int size) 
{ 
    for (int i = 0; i < size; i++) 
    { 
     string[i*2] = bytes[i] < 16 ? '0' : hex_char((bytes[i] >> 4) & 0xF); 
     string[i*2+1] = hex_char(bytes[i] & 0xF); 
    } 
    string[size*2] = 0; 
} 

void setup() 
{ 
    Serial.begin(115200); 
    while (!Serial); 

    randomSeed(analogRead(0)); // Starts the list of random values 
} 

void loop() 
{ 
    randomico(string); // generate random salted string into 'string' variable 
    spritz_hash(hash, 32, (byte*)string, strlen(string)); // hash 'string' into 'hash' variable (hash only the character inside the string not the full char array) 
    bytes_to_hexstr(hash_string, hash, 32); // convert byte hash into printable hex string 

    // print out string and hash 
    Serial.print(string); 
    Serial.print(" -> "); 
    Serial.println(hash_string); 

    delay(1000); 
} 

Mais si vous voulez vraiment utiliser String vous pouvez le faire en tant que tel:

String str = String(random(100))+salt; 
spritz_hash(hash, 32, (byte*)str.c_str(), str.length()); 
+0

Tout d'abord, merci pour l'aide. Je ne comprends tout simplement pas pourquoi il n'imprime pas la chaîne. –

+0

Que voulez-vous dire qu'il n'imprime pas la chaîne? Avez-vous aussi des déchets? Je ne sais pas pourquoi mon code a fonctionné quand je l'ai posté, mais la variable 'string' était trop petite pour contenir 2 octets pour le nombre et 10 octets de sel plus 1 octet pour le caractère nul (était 12 devrait être au moins 13). Maintenant, cela fonctionne comme il se doit. –

+0

Merci pour votre aide. Avec le changement que vous avez fait encore gauche sans imprimer correctement, j'ai fait un changement (char hash_string [66];), vous aviez placé 65. Maintenant, vous faites ce que je voulais. –