2017-01-16 5 views
-1

해시 번호를 무작위로 생성해야하는 프로젝트를 수행하고 있습니다.Arduino에서 숫자와 문자로 구성된 문자열의 해시 생성

코드는 다음과 같이 작동해야합니다.

임의 함수는 임의의 숫자를 생성합니다.이 숫자는 String과 연결되어이 문자열의 해시를 생성합니다. 즉, 숫자가 생성 될 때마다 해당 숫자의 해시도 생성됩니다.이 숫자는 반복되어야합니다. 해시를 생성하려면 SpritzCipher라는 라이브러리를 사용하고 있습니다.

이 코드의 문제점은 String (salt) 값의 연결 해시 대신 "random"단어의 해시를 생성한다는 것입니다.

#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

하는 당신은'testData' 해싱 있습니다,하지만 당신은 valorConcat''에 문자열을 연결합니다. 그리고'const byte testData [12] = {randomico};는 의미가 없습니다. 'randomico' 함수의 주소로 초기화하고 있습니다. 컴파일러가 경고를 주었어야합니다. –

+1

또한 "random"문자열을 실제로 해싱한다는 것을 어떻게 알 수 있습니까? 코드에서 그런 문자열을 인쇄하지 않습니다. 그리고 당신의'testVector'는 문자열''arcfour ''의 해시이고 무작위로 짠 문자열에 대한 해시와 일치하지 않을 것입니다. 'arcfour'를'testData'에 넣더라도,''arcfour \ 0 \ 0 \ 0 \ 0 \ 0 \ ""이 해시 될 것이기 때문에'testVector'가 일치하지 않을 것입니다. 'sizeof' 대신'strlen'을 사용해야합니다. –

+0

이 알고리즘을 사용하여 "void random()"에 의해 생성 된 문자열의 해시를 생성하려면 어떻게해야합니까? 당신은 이것을하는 방법을 알 것입니다 –

답변

0

당신의 해시 코드의 많은 단지이며, 바이트의 배열을 해싱 및 알려진 해시를 비교하는 두 가지 옵션의 예 : 여기

내가했던 코드입니다.

해쉬를 계산할 필요가 있으므로 spritz_hash으로 전화하면됩니다.

String 개체가 동적 메모리 할당을 사용하고 그 문자가 can cause memory problems이므로 문자열 생성에있어보다 효율적인 방법은 저수준 C++ 함수를 사용하는 것입니다.

#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); 
} 

하지만 당신이 정말로 String를 사용하려는 경우 등을 수행 할 수 있습니다

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

우선, 도와 주셔서 감사합니다. 왜 나는 문자열을 인쇄하지 않는지 이해가 안됩니다. –

+0

문자열을 인쇄하지 않는다는 것은 무엇을 의미합니까? 쓰레기도 가져다 주나요? 나는 왜 내가 내 코드를 게시했는지 모르지만'string' 변수가 너무 작아서 숫자 2 바이트, 소금 10 바이트, 널 문자 1 바이트 (12는 13 이상이어야 함)). 이제는 제대로 작동합니다. –

+0

도움 주셔서 감사합니다. 변경 사항을 올바르게 인쇄하지 않고 남겨 뒀다면 65 번 배치 했으므로 (char hash_string [66];) 변경했습니다. 지금 내가 원하는 것을 수행하고 있습니다. –