douyanxing6054 2015-01-20 01:12 采纳率: 0%
浏览 12
已采纳

将数据嵌入序列号中

I am looking at implementing a PVKS as outlined here. I have it working as presented in the article (but in PHP), however I have an extra requirement I'm trying to figure out how to fulfill. I'm trying to figure out how I should embed a date and 3 digit number into the generated code. I'm not even sure where to start here, so I honestly haven't tried anything. They unfortunately won't be unique in combination, so I can't hash them for the seed value. As far as I can tell I can't have them be part of the getKeyByte function either, as the inputs to that function should be only the seed and some arguments, where those arguments define the algorithm for a valid key. They have to be the same between the generator and validator so they have to be static. Is there an accepted practice for this kind of task?

  • 写回答

1条回答 默认 最新

  • dougaimian1143 2015-01-21 04:24
    关注

    Managed to figure this one out myself. I realized I was over complicating things by trying to find a way I could encode a value into a keybyte. The way to do this (well, the way I did this) is just to add extra bytes after the keybytes and before the checksum. They don't need to be validated like the other keybytes, but do affect the checksum.

    Below is the part of my class related to generating and validating keys, with the new sections I needed to add noted.

    static public function generateKey($extraKeyArgs = array(), $encodedData = array())
    {
        $args = self::instanceKeyArgs($extraKeyArgs);
        $keyBytes = array();
        $seed = self::getSeed();
        $hexSeed = self::intToHex($seed,self::$seedWidth);
        $key = $hexSeed;
        $numKeys = count($args);
    
        for ($i=0; $i < $numKeys; $i++) {
            list($a, $b, $c) = $args[$i];
            $keyBytes[$i] = self::getKeyByte($seed, $a, $b, $c, self::$keyWidthBytes);
            $key .= self::intToHex($keyBytes[$i],self::$keyWidthHex);
        }
    
        // Section added to handle encoded data
        foreach ($encodedData as $data) {
            // Make $data an integer value, one byte wide.
            $data = (((int) $data) & 255);
            $keyBytes[] = $data;
            $numKeys++;
            $key .= self::intToHex($data,self::$keyWidthHex);
        }
        // End Section
    
        $checksum = self::getChecksum($key);
    
        $key = $hexSeed . self::$seperator;
        for ($i=0; $i < $numKeys; $i++) {
            $key .= self::intToHex($keyBytes[$i],self::$keyWidthHex);
            if ($i & 1) {
                $key .= self::$seperator;
            }
        }
    
        if (substr($key, -1) !== self::$seperator) {
            $key .= self::$seperator;
        }
    
        $key .= $checksum;
    
        return $key;
    }
    
    static public function checkKey($key, $extraKeyArgs = array(), &$data = array())
    {
        $args = self::instanceKeyArgs($extraKeyArgs);
        $numKeys = count($args);
    
        if (!self::checkKeyChecksum($key)) {
            return false;   // Failed checksum! Maybe a mistype or optical reader error?
        }
    
        $key = self::normalizeKey($key);
    
        // TODO - we would check against a blacklist here if we wanted to implement that.
    
        $seed = hexdec(substr($key,0,self::$seedWidth));
        if (!is_int($seed) || $seed < 1) {
            return false;   // Failed to get seed. Are you sure this key came from here?
        }
    
        $key = substr($key, self::$seedWidth, (strlen($key) - (self::$seedWidth + self::$checksumWidth)));
        for ($i=0; $i < $numKeys; $i++) {
            $keyByte = substr($key, 0, self::$keyWidthHex);
            $key = substr($key, self::$keyWidthHex);
            list($a, $b, $c) = $args[$i];
            if ($keyByte !== self::intToHex(self::getKeyByte($seed, $a, $b, $c, self::$keyWidthBytes),2)) {
                return false;   // Key byte failed check. Possible forgery attempt?
            }
        }
    
        // This line added to handle encoded data
        $data = array_map('hexdec', str_split($key, self::$keyWidthHex));
    
        return true; // Valid Key, Yay!
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog