【问题标题】:Migrating to a new way of identifying devices迁移到识别设备的新方法
【发布时间】:2012-01-14 03:22:03
【问题描述】:

我有一个由设备 udid 标识的用户数据库(在 iOS 5 中已弃用)。我需要一种新方法来识别将不会生成我的数据库中已存在的字符串的设备。

udid 曾经是SHA1(SerialNumber + IMEI + WiFiAddress + BluetoothAddress)

使用MD5(MACAddress) 之类的输出作为我识别设备的新方法是否安全?从我读过的内容来看,MD5 和 SHA-1 似乎输出了不同长度的字符串(分别为 128 位和 160 位),但我只是确保我没有遗漏任何内容。我真的不想以重复的标识符结束......

【问题讨论】:

    标签: iphone ios algorithm security encryption


    【解决方案1】:

    只需使用SHA1(MAC | 0001) 而不是上一个。创建已经存在的 SHA1 的可能性不大,因为这表明 SHA1 算法中存在真正的问题(冲突)。注意:我认为 + 表示连接,我用过 |作为连接。

    如果您需要另一个唯一标识符,您可以在末尾增加计数器(以字符或其他形式,尝试 4 个字节)。

    虽然 MD5 可能足够安全,但我仍会尽量避免使用这种损坏的哈希 - 只使用 SHA1(或移至 SHA-256)。

    【讨论】:

    • 我无权访问任何曾经是 UDID 哈希的一部分的值。我会假设 MAC 地址本身已经是唯一的,我不能只对它进行 MD5 处理,并且由于输出字符串的长度与 SHA1 不同,因此它不会与旧标识符冲突?
    • 同样的事情:如果输入不同,哈希会给你一个唯一的值。再说一遍:MAC 已经是唯一值,所以只有当你想要一个比 MAC 地址更短的值,或者出于隐私原因不想存储 MAC 地址时,你才会这样做。从散列的输出中删除字节显然会增加冲突的机会。我会编辑答案。
    • 为什么是SHA1(MAC | 0001) 而不仅仅是SHA1(MAC)?当您说“破碎的哈希”时,您指的是什么? MD5编码算法?
    • 是的,当我说 MD5 损坏时,我指的是它。在当前情况下这不太可能是一个大问题,但 MD5 可能会失去更多的力量,并且使用真正安全的散列可以更容易地证明正确性。至于柜台:它通常用于例如密钥推导。如果您以后想拥有第二个标识符,这很有用。当然,由于您总是可以添加任何数据以获得不同的哈希值,因此您也可以稍后添加计数器 - 如果需要的话。
    【解决方案2】:

    只需使用设备的 MAC 地址.. 它是唯一的.. 如果您不知道如何获取设备的 MAC 地址,请参阅此代码 -

    #include <sys/socket.h>
    #include <sys/sysctl.h>
    #include <net/if.h>
    #include <net/if_dl.h>
    
    - (NSString *)getMacAddress
    {
      int                 mgmtInfoBase[6];
      char                *msgBuffer = NULL;
      size_t              length;
      unsigned char       macAddress[6];
      struct if_msghdr    *interfaceMsgStruct;
      struct sockaddr_dl  *socketStruct;
      NSString            *errorFlag = NULL;
    
      // Setup the management Information Base (mib)
      mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
      mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
      mgmtInfoBase[2] = 0;
      mgmtInfoBase[3] = AF_LINK;        // Request link layer information
      mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces
    
      // With all configured interfaces requested, get handle index
      if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
        errorFlag = @"if_nametoindex failure";
      else
      {
        // Get the size of the data available (store in len)
        if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
          errorFlag = @"sysctl mgmtInfoBase failure";
        else
        {
          // Alloc memory based on above call
          if ((msgBuffer = malloc(length)) == NULL)
            errorFlag = @"buffer allocation failure";
          else
          {
            // Get system information, store in buffer
            if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
              errorFlag = @"sysctl msgBuffer failure";
          }
        }
      }
    
      // Befor going any further...
      if (errorFlag != NULL)
      {
        NSLog(@"Error: %@", errorFlag);
        return errorFlag;
      }
    
      // Map msgbuffer to interface message structure
      interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
    
      // Map to link-level socket structure
      socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
    
      // Copy link layer address data in socket structure to an array
      memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
    
      // Read from char array into a string object, into traditional Mac address format
      NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
                                    macAddress[0], macAddress[1], macAddress[2],
                                    macAddress[3], macAddress[4], macAddress[5]];
      NSLog(@"Mac Address: %@", macAddressString);
    
      // Release the buffer memory
      free(msgBuffer);
    
      return macAddressString;
    }
    

    这是我关于同一主题的博文 - http://www.makebetterthings.com/iphone/how-to-uniquely-identify-an-ios5-device/

    【讨论】:

    • 我想我应该指定我已经知道如何获取 MAC 地址。我的问题更多是关于 MD5-ing MAC 地址可能与以前的 udid 冲突的可能性。
    • 对 MAC 地址进行 MD5 处理将为您带来全新的体验。它不会与您之前的 udid 冲突
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2021-11-15
    • 2018-06-27
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多