コード例 #1
0
ファイル: MiHomeToken.cs プロジェクト: dlid/Dlid.MiHome
        private void CalculateTokenVector()
        {
            Key = MD5Helper.Hash(Token);

            // Join MD5 of key and the token itself to get the vector
            var ivContent = ByteList.Join(Key, Token);

            InitializationVector = MD5Helper.Hash(ivContent.ToArray());
        }
コード例 #2
0
ファイル: MiHomeRequest.cs プロジェクト: dlid/Dlid.MiHome
        internal byte[] GetPayloadBytes()
        {
            if (_token == null)
            {
                throw new Exception("Token is missing");
            }

            if (_deviceId == null)
            {
                throw new Exception("DeviceID is missing");
            }

            // Encrypt the payload using the token
            var jsonBody         = CreateJson();
            var encryptedPayload = AESHelper.Encrypt(_token.Key, _token.InitializationVector, jsonBody);

            using (var content = new ByteList())
            {
                // Create the header
                content.Add(0x21, 0x31);                                // Magic number
                content.WriteUInt16BE(32 + encryptedPayload.Length);    // Content+header length
                content.Repeat(0x00, 4);                                // Unknown1
                content.AddRange(_deviceId);                            // Device ID (received from handshake)

                // Add seconds passed to server timestamp
                var secondsPassed = (DateTime.Now - _serverStamp.ReceivedTime).TotalSeconds;

                // Write Stamp to payload
                content.WriteUInt32BE((UInt32)Math.Floor(_serverStamp.Timestamp + secondsPassed));

                // Header is done and we have the payload and the token. We can now write the MD5 checksum
                // Get MD5 checksum of [header, token payload]
                content.AddRange(ByteList.Join(content.Take(16).ToArray(), _token.Token, encryptedPayload).ToMd5());

                // Then add the encrypted payload itself
                content.AddRange(encryptedPayload);

                return(content.ToBinaryASCIIArray());
            }
        }