Пример #1
0
 public static ILeoVisitor ToVisitor(this Type type,
                                     AlgorithmKind kind = AlgorithmKind.Precision,
                                     bool repeatable    = RpMode.REPEATABLE,
                                     bool strictMode    = StMode.NORMALE)
 {
     return(LeoVisitorFactory.Create(type, kind, repeatable, strictMode));
 }
Пример #2
0
 public static ILeoVisitor ToVisitor(this Type type, IDictionary <string, object> initialValues,
                                     AlgorithmKind kind = AlgorithmKind.Precision,
                                     bool repeatable    = RpMode.REPEATABLE,
                                     bool strictMode    = StMode.NORMALE)
 {
     return(LeoVisitorFactory.Create(type, initialValues, kind, repeatable, strictMode));
 }
Пример #3
0
        /// <summary>
        /// Create an Object Visitor instance for the given object instance <br />
        /// 为给定的对象实例创建一个 Object Visitor 实例
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="kind"></param>
        /// <param name="repeatable"></param>
        /// <param name="strictMode"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IObjectVisitor <T> Create <T>(T instance, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
        {
            TypeAmGuard.RejectSimpleType <T>();
            var options = FillWith(kind, repeatable, strictMode);

            return(ObjectVisitorFactoryCore.CreateForInstance(instance, options));
        }
Пример #4
0
        public static ICipher GetCipher(AlgorithmKind kind, CipherOptions options)
        {
            ICipher cipher;

            switch (kind)
            {
            case AlgorithmKind.AES:
                cipher = new Cipher(Aes.Create())
                {
                    Options = options
                };
                break;

            case AlgorithmKind.TripleDES:
                cipher = new Cipher(TripleDES.Create())
                {
                    Options = options
                };
                break;

            default:
                throw new NotSupportedException("Unknown algorithm");
            }

            return(cipher);
        }
Пример #5
0
        /// <summary>
        /// Verifies the given data using this Verifier's key.
        /// </summary>
        /// <param name="data">The input data as a string to hash and sign.</param>
        /// <param name="sig">Signature to compare verify as a base64 encoded string.</param>
        /// <param name="encKind">Encoding format of the input data.</param>
        /// <param name="algKind">Hashing algorithm to use.</param>
        /// <returns>True if the given signature matches the input data, otherwise False.</returns>
        public bool Verify(string data, string sig, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1)
        {
            Contract.Requires(!string.IsNullOrEmpty(data));
            Contract.Requires(!string.IsNullOrEmpty(sig));

            return InvokeProvider(encKind, algKind, (rsa, enc, alg) => rsa.VerifyData(enc.GetBytes(data), alg, Convert.FromBase64String(sig)));
        }
Пример #6
0
 public static ILeoVisitor <T> ToVisitor <T>(this T instanceObj,
                                             AlgorithmKind kind = AlgorithmKind.Precision,
                                             bool repeatable    = RpMode.REPEATABLE,
                                             bool strictMode    = StMode.NORMALE)
     where T : class
 {
     return(LeoVisitorFactory.Create(instanceObj, kind, repeatable, strictMode));
 }
Пример #7
0
 public static ILeoVisitor Create(Type type, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
 {
     if (type.IsAbstract && type.IsSealed)
     {
         return(LeoVisitorFactoryCore.CreateForStaticType(type, kind, LvMode.FULL, strictMode));
     }
     return(LeoVisitorFactoryCore.CreateForFutureInstance(type, kind, repeatable, LvMode.FULL, strictMode));
 }
Пример #8
0
 private static ObjectVisitorOptions FillWith(AlgorithmKind kind, bool repeatable, bool strictMode)
 {
     return(ObjectVisitorOptions
            .Default
            .With(x => x.AlgorithmKind = kind)
            .With(x => x.Repeatable = repeatable)
            .With(x => x.StrictMode = strictMode));
 }
Пример #9
0
        public static StaticTypeLeoVisitor <T> CreateForStaticType <T, TStrategy>(AlgorithmKind kind, bool liteMode, bool strictMode)
            where TStrategy : class, ILeoValidationStrategy <T>, new()
        {
            var handler = UnsafeLeoHandleSwitcher.Switch <T>(kind)().With <T>();
            var visitor = new StaticTypeLeoVisitor <T>(handler, kind, liteMode, strictMode);

            visitor.ValidationEntry.SetStrategy <TStrategy>();
            return(visitor);
        }
Пример #10
0
        /// <summary>
        /// Create an Object Visitor instance for the given type <br />
        /// 为给定的类型创建一个 Object Visitor 实例
        /// </summary>
        /// <param name="kind"></param>
        /// <param name="repeatable"></param>
        /// <param name="strictMode"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IObjectVisitor <T> Create <T>(AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
        {
            TypeAmGuard.RejectSimpleType <T>(out var type);
            var options = FillWith(kind, repeatable, strictMode);

            if (type.IsAbstract && type.IsSealed)
            {
                return(ObjectVisitorFactoryCore.CreateForStaticType <T>(options));
            }
            return(ObjectVisitorFactoryCore.CreateForFutureInstance <T>(options));
        }
Пример #11
0
        public static ILeoVisitor <T> Create <T, TStrategy>(AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
            where TStrategy : class, ILeoValidationStrategy <T>, new()
        {
            var type = typeof(T);

            if (type.IsAbstract && type.IsSealed)
            {
                return(LeoVisitorFactoryCore.CreateForStaticType <T, TStrategy>(kind, LvMode.FULL, strictMode));
            }
            return(LeoVisitorFactoryCore.CreateForFutureInstance <T, TStrategy>(kind, repeatable, LvMode.FULL, strictMode));
        }
        public static Func <Type, ObjectCallerBase> Switch(AlgorithmKind kind)
        {
#if NETFRAMEWORK
            return(CompatibleCallerBuilder.Ctor);
#else
            return(kind switch
            {
                AlgorithmKind.Precision => PrecisionDictOperator.CreateFromType,
                AlgorithmKind.Hash => HashDictOperator.CreateFromType,
                AlgorithmKind.Fuzzy => FuzzyDictOperator.CreateFromType,
                _ => throw new InvalidOperationException("Unknown AlgorithmKind.")
            });
Пример #13
0
        public StaticTypeLeoVisitor(DictBase handler, Type targetType, AlgorithmKind kind, bool liteMode = false, bool strictMode = false)
        {
            _handler       = handler ?? throw new ArgumentNullException(nameof(handler));
            _algorithmKind = kind;

            SourceType = targetType ?? throw new ArgumentNullException(nameof(targetType));
            LiteMode   = liteMode;

            _lazyMemberHandler = MemberHandler.Lazy(() => new MemberHandler(_handler, SourceType), liteMode);
            _validationContext = strictMode
                ? new CorrectContext(this, true)
                : null;
        }
Пример #14
0
        protected HashAlgorithm GetAlgorithm(AlgorithmKind algorithmKind)
        {
            HashAlgorithm algorithm;

            switch (algorithmKind)
            {
            case AlgorithmKind.SHA1:
            default:
                algorithm = new SHA1CryptoServiceProvider();
                break;
            }

            return(algorithm);
        }
Пример #15
0
 /// <summary>
 /// 根据实现方式构造MD5算法类(Managed方式未实现)
 /// </summary>
 /// <param name="isCng">算法的实现方式(Managed无效)</param>
 public MD5Crypto(AlgorithmKind kind)
 {
     switch (kind)
     {
         case AlgorithmKind.CryptoServiceProvider:
             _MD5Alg = new MD5CryptoServiceProvider();
             break;
         case AlgorithmKind.Cng:
             _MD5Alg = new MD5Cng();
             break;
         default:
             _MD5Alg = new MD5CryptoServiceProvider();
             break;
     }
 }
Пример #16
0
        /// <summary>
        /// 根据实现方式构造MD5算法类(Managed方式未实现)
        /// </summary>
        /// <param name="isCng">算法的实现方式(Managed无效)</param>
        public MD5Crypto(AlgorithmKind kind)
        {
            switch (kind)
            {
            case AlgorithmKind.CryptoServiceProvider:
                _MD5Alg = new MD5CryptoServiceProvider();
                break;

            case AlgorithmKind.Cng:
                _MD5Alg = new MD5Cng();
                break;

            default:
                _MD5Alg = new MD5CryptoServiceProvider();
                break;
            }
        }
Пример #17
0
        public static Func <Type, DictBase> Switch(AlgorithmKind kind)
        {
            switch (kind)
            {
            case AlgorithmKind.Precision:
                return(PrecisionDictOperator.CreateFromType);

            case AlgorithmKind.Hash:
                return(HashDictOperator.CreateFromType);

            case AlgorithmKind.Fuzzy:
                return(FuzzyDictOperator.CreateFromType);

            default:
                throw new InvalidOperationException("Unknown AlgorithmKind.");
            }
        }
Пример #18
0
        public static Func <DictBase> Switch <T>(AlgorithmKind kind)
        {
            switch (kind)
            {
            case AlgorithmKind.Precision:
                return(() => PrecisionDictOperator <T> .Create());

            case AlgorithmKind.Hash:
                return(() => HashDictOperator <T> .Create());

            case AlgorithmKind.Fuzzy:
                return(() => FuzzyDictOperator <T> .Create());

            default:
                throw new InvalidOperationException("Unknown AlgorithmKind.");
            }
        }
Пример #19
0
        protected T InvokeProvider <T>(EncodingKind encodingKind, AlgorithmKind algorithmKind, Func <RSACryptoServiceProvider, Encoding, HashAlgorithm, T> method)
        {
            var encoding = GetEncoding(encodingKind);

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.PersistKeyInCsp = false;
                if (_isPrivate)
                {
                    rsa.LoadPrivateKeyPEM(_pemKey);
                }
                else
                {
                    rsa.LoadPublicKeyPEM(_pemKey);
                }

                using (var algorithm = GetAlgorithm(algorithmKind))
                    return(method(rsa, encoding, algorithm));
            }
        }
Пример #20
0
        public InstanceVisitor(DictBase <T> handler, T instance, AlgorithmKind kind, bool repeatable,
                               bool liteMode = false, bool strictMode = false)
        {
            _handler       = handler ?? throw new ArgumentNullException(nameof(handler));
            _instance      = instance;
            _algorithmKind = kind;

            _handler.SetInstance(_instance);

            SourceType = typeof(T);
            GenericHistoricalContext = repeatable
                ? new HistoricalContext <T>(kind)
                : null;
            LiteMode = liteMode;

            _lazyMemberHandler = MemberHandler.Lazy(() => new MemberHandler(_handler, SourceType), liteMode);
            _validationContext = strictMode
                ? new CorrectContext <T>(this, true)
                : null;
        }
Пример #21
0
        public static ImageFormat GetBestFittingFormat(Bitmap image, AlgorithmKind algorithm)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            switch (algorithm)
            {
            case AlgorithmKind.ComplexScanning:
                return(GetBestFittingFormatAlgorithm(image));

            case AlgorithmKind.BruteSaving:
                return(GetBestFittingFormatBruteSaving(image));

            case AlgorithmKind.Hybrid:
                return(GetBestFittingFormatHybrid(image));

            default:
                throw new ArgumentException("Impossibru!");
            }
        }
Пример #22
0
        public FutureInstanceVisitor(DictBase handler, Type sourceType, AlgorithmKind kind, bool repeatable,
                                     IDictionary <string, object> initialValues = null, bool liteMode = false, bool strictMode = false)
        {
            _handler       = handler ?? throw new ArgumentNullException(nameof(handler));
            _sourceType    = sourceType ?? throw new ArgumentNullException(nameof(sourceType));
            _algorithmKind = kind;

            _handler.New();
            NormalHistoricalContext = repeatable
                ? new HistoricalContext(sourceType, kind)
                : null;
            LiteMode = liteMode;

            _lazyMemberHandler = MemberHandler.Lazy(() => new MemberHandler(_handler, _sourceType), liteMode);
            _validationContext = strictMode
                ? new CorrectContext(this, true)
                : null;

            if (initialValues != null)
            {
                SetValue(initialValues);
            }
        }
Пример #23
0
 private void Init(string value, AlgorithmKind algorithm)
 {
     Value = value;
     Algorithm = algorithm;
 }
Пример #24
0
 public FluentSetterBuilder(Type type, AlgorithmKind kind)
 {
     _type = type;
     _kind = kind;
 }
Пример #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Hash" /> class from the supplied values.
 /// </summary>
 /// <param name="value">
 /// An initialization value for the <see cref="P: Value" /> property.
 /// </param>
 /// <param name="algorithm">
 /// An initialization value for the <see cref="P: Algorithm" /> property.
 /// </param>
 public Hash(string value, AlgorithmKind algorithm)
 {
     Init(value, algorithm);
 }
Пример #26
0
        /// <summary>
        /// Signs the given data using this Signer's key.
        /// </summary>
        /// <param name="data">The input data as a string to hash and sign.</param>
        /// <param name="encKind">Encoding format of the input data.</param>
        /// <param name="algKind">Hashing algorithm to use.</param>
        /// <returns>The signature of the input data as a base64 encoded string.</returns>
        public string Sign(string data, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1)
        {
            Contract.Requires(!string.IsNullOrEmpty(data));

            return(InvokeProvider(encKind, algKind, (rsa, enc, alg) => Convert.ToBase64String(rsa.SignData(enc.GetBytes(data), alg))));
        }
Пример #27
0
 public static ILeoVisitor Create(Type type, object instance, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
 {
     return(LeoVisitorFactoryCore.CreateForInstance(type, instance, kind, repeatable, LvMode.FULL, strictMode));
 }
Пример #28
0
        /// <summary>
        /// Signs the given data using this Signer's key.
        /// </summary>
        /// <param name="data">The input data as a string to hash and sign.</param>
        /// <param name="encKind">Encoding format of the input data.</param>
        /// <param name="algKind">Hashing algorithm to use.</param>
        /// <returns>The signature of the input data as a base64 encoded string.</returns>
        public string Sign(string data, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1)
        {
            Contract.Requires(!string.IsNullOrEmpty(data));

            return InvokeProvider(encKind, algKind, (rsa, enc, alg) => Convert.ToBase64String(rsa.SignData(enc.GetBytes(data), alg)));
        }
Пример #29
0
 public static ILeoVisitor Create <TStrategy>(Type type, IDictionary <string, object> initialValues, TStrategy validationStrategy, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE,
                                              bool strictMode = StMode.NORMALE)
     where TStrategy : class, ILeoValidationStrategy, new()
 {
     if (type.IsAbstract && type.IsSealed)
     {
         return(LeoVisitorFactoryCore.CreateForStaticType(type, kind, LvMode.FULL, validationStrategy, strictMode));
     }
     return(LeoVisitorFactoryCore.CreateForFutureInstance(type, kind, repeatable, LvMode.FULL, validationStrategy, strictMode, initialValues));
 }
Пример #30
0
 public static ILeoVisitor <T> Create <T, TStrategy>(T instance, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
     where TStrategy : class, ILeoValidationStrategy <T>, new()
 {
     return(LeoVisitorFactoryCore.CreateForInstance <T, TStrategy>(instance, kind, repeatable, LvMode.FULL, strictMode));
 }
Пример #31
0
 public static ILeoVisitor Create <TStrategy>(Type type, object instance, TStrategy validationStrategy, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
     where TStrategy : class, ILeoValidationStrategy, new()
 {
     return(LeoVisitorFactoryCore.CreateForInstance(type, instance, kind, repeatable, LvMode.FULL, validationStrategy, strictMode));
 }
Пример #32
0
        public static ILeoVisitor <T> Create <T>(IDictionary <string, object> initialValues, AlgorithmKind kind = AlgorithmKind.Precision, bool repeatable = RpMode.REPEATABLE, bool strictMode = StMode.NORMALE)
        {
            var type = typeof(T);

            if (type.IsAbstract && type.IsSealed)
            {
                return(LeoVisitorFactoryCore.CreateForStaticType <T>(kind, LvMode.FULL, strictMode));
            }
            return(LeoVisitorFactoryCore.CreateForFutureInstance <T>(kind, repeatable, LvMode.FULL, strictMode, initialValues));
        }
 /// <summary>
 /// According to the given type, create an object of the corresponding type,
 /// and fill the object with the data provided in the dictionary <br />
 /// 根据给定的类型,创建对应类型的对象,并使用字典中提供的数据填充该对象
 /// </summary>
 /// <param name="type"></param>
 /// <param name="keyValueCollections"></param>
 /// <param name="visitor"></param>
 /// <param name="kind"></param>
 /// <returns></returns>
 public static object Create(Type type, IDictionary <string, object> keyValueCollections, out IObjectVisitor visitor, AlgorithmKind kind = AlgorithmKind.Precision)
 {
     visitor = ObjectVisitor.Create(type, keyValueCollections, kind);
     return(visitor.Instance);
 }
 /// <summary>
 /// According to the given type, create an object of the corresponding type,
 /// and fill the object with the data provided in the dictionary <br />
 /// 根据给定的类型,创建对应类型的对象,并使用字典中提供的数据填充该对象
 /// </summary>
 /// <param name="keyValueCollections"></param>
 /// <param name="visitor"></param>
 /// <param name="kind"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static T Create <T>(IDictionary <string, object> keyValueCollections, out IObjectVisitor <T> visitor, AlgorithmKind kind = AlgorithmKind.Precision)
 {
     visitor = ObjectVisitor.Create <T>(keyValueCollections, kind);
     return(visitor.Instance);
 }
 /// <summary>
 /// According to the given type, create an object of the corresponding type,
 /// and fill the object with the data provided in the dictionary <br />
 /// 根据给定的类型,创建对应类型的对象,并使用字典中提供的数据填充该对象
 /// </summary>
 /// <param name="keyValueCollections"></param>
 /// <param name="kind"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static T Create <T>(IDictionary <string, object> keyValueCollections, AlgorithmKind kind = AlgorithmKind.Precision)
 {
     return(Create <T>(keyValueCollections, out _, kind));
 }