Пример #1
0
        /// <summary>
        /// Generated an activation key for the target deviceid.
        /// </summary>
        /// <param name="deviceId">Identifier of the cdeEngine node that is to be activated with the activation key.</param>
        /// <param name="signingKey">Symmetric key (arbitrary string) used to sign the activation key. The same key must be provided on the runtime node.</param>
        /// <param name="Expiration">Expiration date of the activation key. Licenses and entitlements activated using this key will cease to be valid after this date.</param>
        /// <param name="licenses">Note: values in the License.LicenseParameters property will be included in the activation key and added to the values in the license at activation time.
        /// This means that often you will want to set these values to 0 if the license already contains non-zero default paremeters.</param>
        /// <param name="activationKeyParameters">Additional parameters (typically entitlements for thing instances) to be unlocked with this activation key.</param>
        /// <param name="flags">Flags requesting additional activation key behavior.</param>
        /// <returns></returns>
        public static string GenerateActivationKey(Guid deviceId, string signingKey, DateTime Expiration, TheLicense[] licenses, List <TheLicenseParameter> activationKeyParameters, ActivationFlags flags)
        {
            if (licenses.Length > MaxLicensesInActivationKey || licenses.Length == 0)
            {
                return(null);
            }
            var sortedLicenses = licenses.OrderBy((l) => l.LicenseId.ToString());

            byte[] licenseSignature;
            byte[] licenseParams = GenerateLicenseParameters(sortedLicenses);
            if (licenseParams == null || licenseParams.Length > TheActivationUtils.MaxLicenseParameters)
            {
                return(null);
            }
            int index = 0;

            foreach (var param in activationKeyParameters)
            {
                licenseParams[index] = param.Value;
                index++;
            }


            double expirationTemp = Math.Ceiling((Expiration.ToUniversalTime() - new DateTime(2016, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalDays);

            if (expirationTemp < 0 || expirationTemp > 0xFFFF)
            {
                return(null);
            }
            uint expirationInDays = (uint)expirationTemp;

            if (!TheActivationUtils.GenerateLicenseSignature(deviceId, signingKey, expirationInDays, sortedLicenses.ToArray(), licenseParams, flags, out licenseSignature))
            {
                return(null);
            }
            byte[] activationKey = new byte[23];
            // 0-7: HMACSHA1 (appId, licenseAuthorization) -> truncate to 8 bytes (64 bits)
            licenseSignature.Take(8).ToArray().CopyTo(activationKey, 0);
            // 8-9: Expiration: Days since January 2016. Resulting Range: 2016 - 2195
            activationKey[8] = (byte)(expirationInDays & 0xFF);
            activationKey[9] = (byte)((expirationInDays >> 8) & 0xFF);
            // 10: Flags
            activationKey[10] = (byte)flags;
            // 11: Reserved (0)
            activationKey[11] = 0;
            // 12: License count
            activationKey[12] = (byte)sortedLicenses.Count();
            // 13-21: License parameters (
            licenseParams.CopyTo(activationKey, 13);
            // 22: Padding
            activationKey[22] = 15; // Ensure we always get 6x6 characters in base32

            var key = TheActivationUtils.Base32Encode(activationKey);

            //var testKey = TheActivationUtils.Base32Decode(key.Replace("-",""));
            return(key);
        }
Пример #2
0
        public static string GetActivationRequestKey(Guid deviceId, uint SkuId)
        {
            if (deviceId == Guid.Empty)
            {
                //TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ThingService, "Unable to generate activation key: device id not configured.", eMsgLevel.l2_Warning, ""));
                return(null);
            }
            byte[] tGu = deviceId.ToByteArray();
            uint   tenMinuteIntervalsSinceUnixEpoch = (uint)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMinutes) / 10;

            // Base 32 encoded, 22 Bytes:
            // 0,2,4 - Creation Time (10 Minute intervals since Unix Epoch, little endian)
            // 6,8 - SkuId (little endian)
            // 1,3,5,7,9-20 - Device Id (16 byte GUID)
            // 21 - Checksum
            // 22 - Padding (to ensure 6x6 chars in base32)
            byte[] activationRequestKey = new byte[3 + tGu.Length + 2 + 1 + 1];

            int j = 0;
            //activationRequestKey[0] = (byte) (DateTime.UtcNow.Ticks & 0xff);
            //j++;

            byte checksum = 0;// activationRequestKey[0];

            for (int i = 0; i < tGu.Length; i++)
            {
                if (i < 3)
                {
                    activationRequestKey[j]          = (byte)((tenMinuteIntervalsSinceUnixEpoch & 0xff) ^ checksum);
                    tenMinuteIntervalsSinceUnixEpoch = tenMinuteIntervalsSinceUnixEpoch >>= 8;
                    checksum += activationRequestKey[j];
                    j++;
                }
                else if (i < 5)
                {
                    activationRequestKey[j] = (byte)((SkuId & 0xff) ^ checksum);
                    SkuId     = SkuId >>= 8;
                    checksum += activationRequestKey[j];
                    j++;
                }
                activationRequestKey[j] = (byte)(tGu[i] ^ checksum);
                checksum += activationRequestKey[j];
                j++;
            }
            activationRequestKey[j] = (byte)(checksum & 0xff);
            j++;
            //activationRequestKey[j + 1] = (byte) ((checksum >> 8) + activationRequestKey[0]);
            //activationRequestKey[j + 2] =  (byte) (((activationRequestKey[0]) | 0x02) & 0x03); // ensure positive big int and 36 chars in output

            activationRequestKey[j] = 15; // padding
            return(TheActivationUtils.Base32Encode(activationRequestKey));
        }
        public static string CreateActivationRequestKey(Guid deviceId, uint SkuId)
        {
            if (deviceId == Guid.Empty)
            {
                return(null);
            }
            byte[] tGu = deviceId.ToByteArray();
            uint   tenMinuteIntervalsSinceUnixEpoch = (uint)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMinutes) / 10;

            // Base 32 encoded, 22 Bytes:
            // 0,2,4 - Creation Time (10 Minute intervals since Unix Epoch, little endian)
            // 6,8 - SkuId (little endian)
            // 1,3,5,7,9-20 - Device Id (16 byte GUID)
            // 21 - Checksum
            // 22 - Padding (to ensure 6x6 chars in base32)
            byte[] activationRequestKey = new byte[3 + tGu.Length + 2 + 1 + 1];

            int j = 0;
            //activationRequestKey[0] = (byte) (DateTime.UtcNow.Ticks & 0xff);
            //j++;

            byte checksum = 0;// activationRequestKey[0];

            for (int i = 0; i < tGu.Length; i++)
            {
                if (i < 3)
                {
                    activationRequestKey[j]          = (byte)((tenMinuteIntervalsSinceUnixEpoch & 0xff) ^ checksum);
                    tenMinuteIntervalsSinceUnixEpoch = tenMinuteIntervalsSinceUnixEpoch >>= 8;
                    checksum += activationRequestKey[j];
                    j++;
                }
                else if (i < 5)
                {
                    activationRequestKey[j] = (byte)((SkuId & 0xff) ^ checksum);
                    SkuId     = SkuId >>= 8;
                    checksum += activationRequestKey[j];
                    j++;
                }
                activationRequestKey[j] = (byte)(tGu[i] ^ checksum);
                checksum += activationRequestKey[j];
                j++;
            }
            activationRequestKey[j] = (byte)(checksum & 0xff);
            j++;
            //activationRequestKey[j + 1] = (byte) ((checksum >> 8) + activationRequestKey[0]);
            //activationRequestKey[j + 2] =  (byte) (((activationRequestKey[0]) | 0x02) & 0x03); // ensure positive big int and 36 chars in output

            activationRequestKey[j] = 15; // padding
            return(TheActivationUtils.Base32Encode(activationRequestKey));
        }