【问题标题】:Device’s Activation Code best practice设备的激活码最佳实践
【发布时间】:2013-08-27 23:03:29
【问题描述】:

引用文档:

预先注册后,设备可以通过向 Xively API 发送激活码来激活。这向 Xively 发出信号,表明设备已首次唤醒,并请求为其提供可使用的 Feed ID 和 API 密钥。设备的激活码是使用 HMAC-SHA1 散列生成的,该散列将设备的序列号与其父产品的产品秘密相结合,使某人实际上不可能从激活码中提取产品秘密,或在配置中冒充设备过程。

什么是最佳实践:

  1. 将激活码保存在每个设备内存中:在出厂时进行编程非常耗时
  2. 通过HMAC-SHA1(serialnumber, productid)计算设备唤醒时的激活码。

在我的情况下,第二个更有意义,但是我无法从 API 文档中找到 HMAC 是如何计算的。它只是一个字符串连接吗?填充呢?

【问题讨论】:

    标签: provisioning xively


    【解决方案1】:

    errordeveloper 所说的一切都是绝对正确的。

    另外要记住的是,产品(或设备)页面上列出的产品密码已经是十六进制对格式。您不需要将字符串转换为十六进制,而是将当前字符串用作十六进制字符串。您可以在 errordeveloper 发布的 Arduino 代码中看到这是如何完成的。

    【讨论】:

    • 太好了,我会用 Python 试一试并分享我的工作代码。
    【解决方案2】:

    an example 介绍了如何在 Python 中完成此操作。您会注意到它使用binascii .a2b_hex() 将产品机密转换为二进制。

    这是 Ruby 中的另一个示例:

    require('openssl')
    
    secret = '488f40ff3d0b2c6188e272f8d86416b73b3cb4ef'
    serial = '0123456789'
    
    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 = "0123456789";
    String activation_code;
    
    String convertHash(uint8_t* hash) {
      String returnString;
      int i;
      for (i=0; i<20; i++) {
        returnString += ("0123456789abcdef"[hash[i]>>4]);
        returnString += ("0123456789abcdef"[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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      相关资源
      最近更新 更多