2013-08-26 62 views
0

引用文檔:設備的激活碼最佳實踐

一旦預註冊,設備可以通過發送激活碼的Xively API激活。這告訴Xively設備第一次被喚醒,並且正在請求提供它可以使用的Feed ID和API密鑰。設備的激活碼使用HMAC-SHA1散列生成,該HMAC-SHA1散列將設備的序列號與其父產品的產品密鑰結合在一起,以使某人無法從激活碼中提取產品密鑰,或者在供應中欺騙性地冒充設備處理。

什麼是最好的做法:

  1. 保持激活碼上的每個設備內存:很長的時間在出廠時
  2. 耗時程序通過HMAC-SHA1(serialnumber, productid)計算設備上喚醒激活代碼。

在我的情況下,第二個更有意義,但我無法找到HMAC是如何從API文檔計算的。它只是一個字符串連接?怎麼樣填充?

回答

1

一切errordeveloper說是絕對正確的。

要記住的另一件事是產品(或設備)頁面上列出的產品密鑰已經是十六進制格式。您不需要將字符串轉換爲十六進制,而是將當前字符串用作十六進制字符串。您可以在errordeveloper發佈的Arduino代碼中看到這是如何完成的。

+0

非常好我會在Python中給它一個,並分享我的工作代碼。 – user2471214

1

有關於如何在Python中完成的an example。您會注意到它使用binascii .a2b_hex()將產品密鑰轉換爲二進制文件。

這裏是Ruby的另一個例子:

require('openssl') 

secret = '488f40ff3d0b2c6188e272f8d86416b73b3cb4ef' 
serial = '' 

digest = OpenSSL::Digest::Digest.new('sha1') 
puts OpenSSL::HMAC.hexdigest(digest, [secret].pack("H*"), serial) 

這裏是一個Arduino的:

// First, download the library from https://github.com/Cathedrow/Cryptosuite 
// and make sure it is installed properly, although until it supports Arduino 1.0, 
// it's better to use this fork: https://github.com/jkiv/Cryptosuite 
#include "sha1.h" 

uint8_t secret[]={ 
    0x48,0x8f,0x40,0xff,0x3d,0x0b,0x2c,0x61,0x88,0xe2, 
    0x72,0xf8,0xd8,0x64,0x16,0xb7,0x3b,0x3c,0xb4,0xef, 
}; 
String serial = ""; 
String activation_code; 

String convertHash(uint8_t* hash) { 
    String returnString; 
    int i; 
    for (i=0; i<20; i++) { 
    returnString += ("abcdef"[hash[i]>>4]); 
    returnString += ("abcdef"[hash[i]&0xf]); 
    } 
    return returnString; 
} 

void setup() { 
    // Generally you would compute the hash once on start up and store it in flash 
    // to avoid doing it each time as it can be a bit slow 
    Serial.begin(57600); 
    Serial.println("computing hmac sha: "); 
    Sha1.initHmac(secret, 20); 
    Sha1.print(serial); 
    activation_code = convertHash(Sha1.resultHmac()); 
    Serial.println(activation_code); 
} 
+0

你好,python代碼非常好,現在很好用! – user2471214