コード例 #1
0
        public void Set(string key, byte[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (IsAvailable)
            {
                var encodedKey = new EncodedKey(key);
                if (encodedKey.KeyBytes.Length > KeyLengthLimit)
                {
                    throw new ArgumentOutOfRangeException(nameof(key), "Exception_KeyLengthIsExceeded");
                }

                if (!this.tryEstablishSession())
                {
                    throw new InvalidOperationException("Exception_InvalidSessionEstablishment");
                }
                this.isModified = true;
                byte[] copy = new byte[value.Length];
                Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
                this.store[encodedKey] = copy;
            }
        }
コード例 #2
0
    /// <inheritdoc />
    public void Set(string key, byte[] value)
    {
        if (value == null)
        {
            throw new ArgumentNullException(nameof(value));
        }

        if (IsAvailable)
        {
            var encodedKey = new EncodedKey(key);
            if (encodedKey.KeyBytes.Length > KeyLengthLimit)
            {
                throw new ArgumentOutOfRangeException(nameof(key),
                                                      Resources.FormatException_KeyLengthIsExceeded(KeyLengthLimit));
            }

            if (!_tryEstablishSession())
            {
                throw new InvalidOperationException(Resources.Exception_InvalidSessionEstablishment);
            }
            _isModified = true;
            var copy = new byte[value.Length];
            Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
            _store.SetValue(encodedKey, copy);
        }
    }
コード例 #3
0
        private void Deserialize(Stream content)
        {
            if (content == null || content.ReadByte() != SerializationRevision)
            {
                // Replace the un-readable format.
                this.isModified = true;
                return;
            }

            int expectedEntries = DeserializeNumFrom3Bytes(content);

            this.sessionIdBytes = ReadBytes(content, IdByteCount);

            for (int i = 0; i < expectedEntries; i++)
            {
                int keyLength  = DeserializeNumFrom2Bytes(content);
                var key        = new EncodedKey(ReadBytes(content, keyLength));
                int dataLength = DeserializeNumFrom4Bytes(content);
                this.store[key] = ReadBytes(content, dataLength);
            }

            if (this.logger.IsEnabled(LogLevel.Debug))
            {
                this.sessionId = new Guid(this.sessionIdBytes).ToString();
                //this.logger.SessionLoaded(_sessionKey, _sessionId, expectedEntries);
            }
        }
コード例 #4
0
    private void Deserialize(Stream content)
    {
        if (content == null || content.ReadByte() != SerializationRevision)
        {
            // Replace the un-readable format.
            _isModified = true;
            return;
        }

        var expectedEntries = DeserializeNumFrom3Bytes(content);

        _sessionIdBytes = ReadBytes(content, IdByteCount);

        for (var i = 0; i < expectedEntries; i++)
        {
            var keyLength  = DeserializeNumFrom2Bytes(content);
            var key        = new EncodedKey(ReadBytes(content, keyLength));
            var dataLength = DeserializeNumFrom4Bytes(content);
            _store.SetValue(key, ReadBytes(content, dataLength));
        }

        if (_logger.IsEnabled(LogLevel.Debug))
        {
            _sessionId = new Guid(_sessionIdBytes).ToString();
            _logger.SessionLoaded(_sessionKey, _sessionId, expectedEntries);
        }
    }
コード例 #5
0
 void when_encoding()
 {
     context["hello, version 0"] = () =>
     {
         before = () => key = new EncodedKey(0, new byte[] { 0x68, 0x65, 0x6c, 0x6c, 0x6f });
         it["should become 12L5B5yqsf7vwb"] = () => key.ToString().should_be("12L5B5yqsf7vwb");
     };
 }
コード例 #6
0
ファイル: EcKey.cs プロジェクト: viruswevh/ObscurCore
 /// <inheritdoc />
 public override int GetHashCode()
 {
     unchecked {
         int hashCode = PublicComponent.GetHashCode();
         hashCode = (hashCode * 397) ^ CurveProviderName.ToLowerInvariant().GetHashCode();
         hashCode = (hashCode * 397) ^ CurveName.ToLowerInvariant().GetHashCode();
         hashCode = (hashCode * 397) ^ EncodedKey.GetHashCodeExt();
         hashCode = (hashCode * 397) ^ (AdditionalData != null ? AdditionalData.GetHashCodeExt() : 0);
         hashCode = (hashCode * 397) ^ (ConfirmationCanary != null ? ConfirmationCanary.GetHashCodeExt() : 0);
         return(hashCode);
     }
 }
コード例 #7
0
        void when_decoding()
        {
            context["12L5B5yqsf7vwb"] = () =>
            {
                before = () => key = new EncodedKey("12L5B5yqsf7vwb");
                it["should become hello"]   = () => key.RawKey.should_be(new byte[] { 0x68, 0x65, 0x6c, 0x6c, 0x6f });
                it["should have version 0"] = () => key.Version.should_be(0);
            };

            context["mistyped key"] = () =>
            {
                it["should throw exception"] = expect <ArgumentException>("Checksum is invalid",
                                                                          () => new EncodedKey("12L5B5ygsf7vwb"));
            };
        }
コード例 #8
0
ファイル: EcKey.cs プロジェクト: viruswevh/ObscurCore
 /// <inheritdoc />
 public bool Equals(ECKey other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(PublicComponent == other.PublicComponent &&
            String.Equals(CurveProviderName, other.CurveProviderName, StringComparison.OrdinalIgnoreCase) &&
            String.Equals(CurveName, other.CurveName, StringComparison.OrdinalIgnoreCase) &&
            EncodedKey.SequenceEqualShortCircuiting(other.EncodedKey) &&
            AdditionalData == null ? other.AdditionalData == null : AdditionalData.SequenceEqualShortCircuiting(other.AdditionalData) &&
            ConfirmationCanary == null ? other.ConfirmationCanary == null : ConfirmationCanary.SequenceEqualShortCircuiting(other.ConfirmationCanary));
 }
コード例 #9
0
ファイル: DistributedSession.cs プロジェクト: jamiuaz/Session
        public void Set(string key, [NotNull] byte[] value)
        {
            var encodedKey = new EncodedKey(key);
            if (encodedKey.KeyBytes.Length > KeyLengthLimit)
            {
                throw new ArgumentOutOfRangeException(nameof(key),
                    string.Format("The key cannot be longer than '{0}' when encoded with UTF-8.", KeyLengthLimit));
            }

            Load();
            if (!_tryEstablishSession())
            {
                throw new InvalidOperationException("The session cannot be established after the response has started.");
            }
            _isModified = true;
            byte[] copy = new byte[value.Length];
            Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
            _store[encodedKey] = copy;
        }
コード例 #10
0
        public void Set(string key, [NotNull] byte[] value)
        {
            var encodedKey = new EncodedKey(key);

            if (encodedKey.KeyBytes.Length > KeyLengthLimit)
            {
                throw new ArgumentOutOfRangeException(nameof(key),
                                                      string.Format("The key cannot be longer than '{0}' when encoded with UTF-8.", KeyLengthLimit));
            }

            Load();
            if (!_tryEstablishSession())
            {
                throw new InvalidOperationException("The session cannot be established after the response has started.");
            }
            _isModified = true;
            byte[] copy = new byte[value.Length];
            Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
            _store[encodedKey] = copy;
        }
コード例 #11
0
        private void Deserialize(Stream content)
        {
            if (content == null || content.ReadByte() != SerializationRevision)
            {
                // TODO: Throw?
                // Replace the un-readable format.
                _isModified = true;
                return;
            }

            int expectedEntries = DeserializeNumFrom3Bytes(content);

            for (int i = 0; i < expectedEntries; i++)
            {
                int keyLength  = DeserializeNumFrom2Bytes(content);
                var key        = new EncodedKey(content.ReadBytes(keyLength));
                int dataLength = DeserializeNumFrom4Bytes(content);
                _store[key] = content.ReadBytes(dataLength);
            }
        }
コード例 #12
0
        public void Set(string key, ArraySegment <byte> value)
        {
            var encodedKey = new EncodedKey(key);

            if (encodedKey.KeyBytes.Length > KeyLengthLimit)
            {
                throw new ArgumentOutOfRangeException("key", key,
                                                      string.Format("The key cannot be longer than '{0}' when encoded with UTF-8.", KeyLengthLimit));
            }
            if (value.Array == null)
            {
                throw new ArgumentException("The ArraySegment<byte>.Array cannot be null.", "value");
            }

            Load();
            if (!_tryEstablishSession())
            {
                throw new InvalidOperationException("The session cannot be established after the response has started.");
            }
            _isModified = true;
            byte[] copy = new byte[value.Count];
            Buffer.BlockCopy(value.Array, value.Offset, copy, 0, value.Count);
            _store[encodedKey] = copy;
        }
コード例 #13
0
ファイル: DistributedSession.cs プロジェクト: yaobos/Session
        public void Set(string key, byte[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var encodedKey = new EncodedKey(key);
            if (encodedKey.KeyBytes.Length > KeyLengthLimit)
            {
                throw new ArgumentOutOfRangeException(nameof(key),
                    Resources.FormatException_KeyLengthIsExceeded(KeyLengthLimit));
            }

            Load();
            if (!_tryEstablishSession())
            {
                throw new InvalidOperationException(Resources.Exception_InvalidSessionEstablishment);
            }
            _isModified = true;
            byte[] copy = new byte[value.Length];
            Buffer.BlockCopy(src: value, srcOffset: 0, dst: copy, dstOffset: 0, count: value.Length);
            _store[encodedKey] = copy;
        }
コード例 #14
0
ファイル: DistributedSession.cs プロジェクト: yaobos/Session
        private void Deserialize(Stream content)
        {
            if (content == null || content.ReadByte() != SerializationRevision)
            {
                // TODO: Throw?
                // Replace the un-readable format.
                _isModified = true;
                return;
            }

            int expectedEntries = DeserializeNumFrom3Bytes(content);
            for (int i = 0; i < expectedEntries; i++)
            {
                int keyLength = DeserializeNumFrom2Bytes(content);
                var key = new EncodedKey(ReadBytes(content, keyLength));
                int dataLength = DeserializeNumFrom4Bytes(content);
                _store[key] = ReadBytes(content, dataLength);
            }
        }
コード例 #15
0
        public void Set(string key, ArraySegment<byte> value)
        {
            var encodedKey = new EncodedKey(key);
            if (encodedKey.KeyBytes.Length > KeyLengthLimit)
            {
                throw new ArgumentOutOfRangeException("key", key,
                    string.Format("The key cannot be longer than '{0}' when encoded with UTF-8.", KeyLengthLimit));
            }
            if (value.Array == null)
            {
                throw new ArgumentException("The ArraySegment<byte>.Array cannot be null.", "value");
            }

            Load();
            if (!_tryEstablishSession())
            {
                throw new InvalidOperationException("The session cannot be established after the response has started.");
            }
            _isModified = true;
            byte[] copy = new byte[value.Count];
            Buffer.BlockCopy(value.Array, value.Offset, copy, 0, value.Count);
            _store[encodedKey] = copy;
        }
コード例 #16
0
ファイル: NoOpSessionStore.cs プロジェクト: pa-at/aspnetcore
 public bool TryGetValue(EncodedKey key, [MaybeNullWhen(false)] out byte[] value)
 {
     value = null;
     return(false);
 }
コード例 #17
0
ファイル: NoOpSessionStore.cs プロジェクト: pa-at/aspnetcore
 public bool Remove(EncodedKey key) => false;
コード例 #18
0
ファイル: NoOpSessionStore.cs プロジェクト: pa-at/aspnetcore
 public void SetValue(EncodedKey key, byte[] value)
 {
 }
コード例 #19
0
 public bool Remove(EncodedKey encodedKey)
 => _store.Remove(encodedKey);
コード例 #20
0
 public void SetValue(EncodedKey key, byte[] value) => _store[key] = value;
コード例 #21
0
 public bool TryGetValue(EncodedKey key, [MaybeNullWhen(false)] out byte[] value)
 => _store.TryGetValue(key, out value);