Skip to content

Instantly share code, notes, and snippets.

@ccckmit
Created June 5, 2018 23:13
Show Gist options
  • Save ccckmit/aed11096bf44760569423dd01b16f491 to your computer and use it in GitHub Desktop.
Save ccckmit/aed11096bf44760569423dd01b16f491 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
#include "sha1.c"
void printNow() { // 印出目前時間
time_t now = time(NULL);
struct tm *tmNow = (struct tm*) localtime(&now);
printf ("Current local time and date: %s", asctime(tmNow));
}
int main (int argc, char **argv) {
sha1nfo s; // 呼叫 SHA-1 函數所需要的資料結構。
char msg[1000]; // 需經 SHA-1 進行 hashcash 認證的郵件。
char *head = "from:abc@gmail.com to:ccckmit@gmail.com title=hello! nonce=%d"; // 郵件樣式
unsigned int nonce = 0; // 可嵌入在郵件中並通過認證的值,稱為 nonce。
printNow(); // 印出起始時間
for (nonce = 0; nonce < UINT_MAX; nonce++) { // 從零開始一直往上找 nonce 值
sprintf(msg, head, nonce); // 將 nonce 嵌入樣版文件中,取得此次測試的郵件訊息。
sha1_init(&s); // 準備開始進行 SHA-1 雜湊。
sha1_write(&s, msg, strlen(msg)); // 將訊息加入,以便進行 SHA-1 雜湊。
uint8_t* hash = sha1_result(&s); // 開始進行 SHA-1 雜湊,計算出雜湊值 hash。
if (hash[0] == 0 && hash[1]==0 && hash[2]==0) { // 如果雜湊值的前 24bit (3 個 byte) 都是零,那麼就符合了。
printf("msg=%s\n", msg); // 印出訊息內容。
printf("hash="); // 印出雜湊欄位名稱。
printHash(hash); // 印出雜湊欄位內容。
break;
}
}
printNow(); // 印出完成時間
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment