public FileToken Decode(string tokenStr)
        {
            var encBys = _urlDataCodec.Decode(tokenStr);

            //校验签名
            var hashLen = 16;
            var mdatBys = new byte[encBys.Length - hashLen];

            Array.Copy(encBys, hashLen, mdatBys, 0, mdatBys.Length);
            var signBys = ArrayUtil.Addition(_appSecretBytes, mdatBys);
            var hashBys = Md5(signBys);

            if (!ArrayUtil.Equals(hashBys, 0, encBys, 0, hashLen))
            {
                throw new InvalidDataException("bad sign");
            }

            if (mdatBys[0] != CurrentVersion)
            {
                throw new NotSupportedException("bad token version");
            }

            //解析成对象
            var index    = 1; //忽略版本
            var pseudoId = NetBitConverter.ToUInt32(mdatBys, index);

            index += 4;
            var fileId = NetBitConverter.ToInt32(mdatBys, index);

            index += 4;
            var ownerId = NetBitConverter.ToInt32(mdatBys, index);

            index += 4;
            var mimeId = NetBitConverter.ToUInt32(mdatBys, index);

            index += 4;
            var expireTime = ToDateTime(mdatBys, index);

            index += sizeof(long);
            var fileCreateTime = ToDateTime(mdatBys, index);

            return(new FileToken
            {
                PseudoId = pseudoId,
                FileId = fileId,
                FileOwnerId = ownerId,
                MimeId = mimeId,
                ExpireTime = expireTime,
                FileCreateTime = fileCreateTime
            });
        }
        public OwnerToken Decode(string tokenStr)
        {
            var encBys = _urlDataCodec.Decode(tokenStr);

            //校验签名
            var hashLen = 20;
            var mdatBys = new byte[encBys.Length - hashLen];

            Array.Copy(encBys, hashLen, mdatBys, 0, mdatBys.Length);
            var signBys = ArrayUtil.Addition(_appSecretBytes, mdatBys);
            var hashBys = Sha1(signBys);

            if (!ArrayUtil.Equals(hashBys, 0, encBys, 0, hashLen))
            {
                throw new InvalidDataException("bad sign");
            }

            if (mdatBys[0] != CurrentVersion)
            {
                throw new NotSupportedException("bad token version");
            }

            //解析成对象
            var index     = 1; //忽略版本
            var ownerType = NetBitConverter.ToInt32(mdatBys, index);

            index += 4;
            var ownerId = NetBitConverter.ToInt32(mdatBys, index);

            index += 4;
            var expireTime = ToDateTime(mdatBys, index);

            return(new OwnerToken
            {
                OwnerType = ownerType,
                OwnerId = ownerId,
                ExpireTime = expireTime
            });
        }
Ejemplo n.º 3
0
        private async ValueTask <long> ReadCommitTimeOffset(uint index)
        {
            var offsetData = new byte[sizeof(int)];

            if (offsetData.Length != await ReadFromChunkAsync("GDA2", index * sizeof(int), offsetData).ConfigureAwait(false))
            {
                return(long.MinValue);
            }

            int v = NetBitConverter.ToInt32(offsetData, 0);

            if (v >= 0)
            {
                return(v);
            }

            offsetData = new byte[sizeof(long)];
            if (offsetData.Length != await ReadFromChunkAsync("GDO2", (~v) * sizeof(long), offsetData).ConfigureAwait(false))
            {
                return(long.MinValue);
            }

            return(NetBitConverter.ToInt64(offsetData, 0));
        }
Ejemplo n.º 4
0
        private async ValueTask <bool> RefillAsync(bool allowWait)
        {
            if (_state <= ewah_state.start && !allowWait && Inner.Peek().IsEmpty)
            {
                return(false);
            }

            if (_lengthBits is null)
            {
                var bb = await Inner.ReadFullAsync(4 + 4).ConfigureAwait(false);

                _lengthBits     = NetBitConverter.ToUInt32(bb, 0);
                _compressedSize = NetBitConverter.ToInt32(bb, 4);

                _left  = _compressedSize;
                _state = ewah_state.start;
            }

            int peekLength = Inner.Peek().Length / sizeof(ulong);

            _wpos = 0;

            switch (_state)
            {
            case ewah_state.start:
                ulong curOp = await Inner.ReadNetworkUInt64Async().ConfigureAwait(false);

                _repBit   = (curOp & 1UL) != 0;
                _repCount = (uint)(curOp >> 1);
                _rawCount = (int)(curOp >> 33);

                _left--;
                peekLength--;
                _state = ewah_state.same;
                goto case ewah_state.same;

            case ewah_state.same:
                byte val = _repBit ? (byte)0xFF : (byte)0;
                while (_repCount > 0 && _wpos + 8 < _buffer.Length)
                {
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _buffer[_wpos++] = val;
                    _repCount--;
                }
                if (_repCount > 0)
                {
                    _readable = new BucketBytes(_buffer, 0, _wpos);
                    return(true);
                }

                _state = ewah_state.raw;
                goto case ewah_state.raw;

            case ewah_state.raw:
                while (_rawCount > 0)
                {
                    if ((_wpos > 8 && peekLength < 8) || (_wpos + 8 >= _buffer.Length))
                    {
                        // Avoid new reads if we already have something. Return result
                        _readable = new BucketBytes(_buffer, 0, _wpos);
                        return(true);
                    }

                    var bb = await Inner.ReadFullAsync(sizeof(ulong)).ConfigureAwait(false);

                    if (bb.Length != sizeof(ulong))
                    {
                        throw new BucketEofException(Inner);
                    }

                    peekLength--;
                    _left--;
                    _rawCount--;

                    for (int i = bb.Length - 1; i >= 0; i--)
                    {
                        _buffer[_wpos++] = bb[i];
                    }
                }

                if (_left == 0)
                {
                    _state    = ewah_state.footer;
                    _readable = new BucketBytes(_buffer, 0, _wpos);
                    return(true);
                }

                _state = ewah_state.start;
                goto case ewah_state.start;

            case ewah_state.footer:
                await Inner.ReadNetworkUInt32Async().ConfigureAwait(false);

                _state = ewah_state.done;
                goto case ewah_state.done;

            case ewah_state.done:
            default:
                return(false);
            }
        }