Ejemplo n.º 1
0
        /// <summary>
        /// Returns a platform-specific algorithm that conforms to the prescribed platform-neutral algorithm.
        /// </summary>
        /// <param name="algorithm">The PCL algorithm.</param>
        /// <returns>
        /// The platform-specific algorithm.
        /// </returns>
        private static Platform.SymmetricAlgorithm GetAlgorithm(SymmetricAlgorithm algorithm)
        {
#if SILVERLIGHT || __IOS__
            switch (algorithm)
            {
            case SymmetricAlgorithm.AesCbcPkcs7:
                return(new Platform.AesManaged());

            default:
                throw new NotSupportedException();
            }
#else
            Platform.SymmetricAlgorithm platform = Platform.SymmetricAlgorithm.Create(
                algorithm.GetName().GetString());
            if (platform == null)
            {
                throw new NotSupportedException();
            }

            platform.Mode    = GetMode(algorithm);
            platform.Padding = GetPadding(algorithm);

            return(platform);
#endif
        }
        /// <summary>
        /// Gets the block size (in bytes) for the specified algorithm.
        /// </summary>
        /// <param name="pclAlgorithm">The PCL algorithm.</param>
        /// <param name="algorithm">The platform-specific algorithm.</param>
        /// <returns>The block size (in bytes).</returns>
        internal static int GetBlockSize(SymmetricAlgorithm pclAlgorithm, Cipher algorithm)
        {
            Requires.NotNull(algorithm, "algorithm");

            if (algorithm.BlockSize == 0 && pclAlgorithm.GetName() == SymmetricAlgorithmName.Rc4)
            {
                // This is a streaming cipher without a block size. Return 1 to emulate behavior of other platforms.
                return(1);
            }

            return(algorithm.BlockSize);
        }
Ejemplo n.º 3
0
        /// <summary>Returns a crypto key management for a specified algorithm.</summary>
        /// <param name="factory">The factory.</param>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>An instance of <see cref="ISymmetricKeyAlgorithmProvider"/>.</returns>
        public static ISymmetricKeyAlgorithmProvider OpenAlgorithm(this ISymmetricKeyAlgorithmProviderFactory factory, SymmetricAlgorithm algorithm)
        {
            Requires.NotNull(factory, nameof(factory));

            return(factory.OpenAlgorithm(algorithm.GetName(), algorithm.GetMode(), algorithm.GetPadding()));
        }
        /// <summary>
        /// Gets the block size (in bytes) for the specified algorithm.
        /// </summary>
        /// <param name="pclAlgorithm">The PCL algorithm.</param>
        /// <param name="algorithm">The platform-specific algorithm.</param>
        /// <returns>The block size (in bytes).</returns>
        internal static int GetBlockSize(SymmetricAlgorithm pclAlgorithm, Cipher algorithm)
        {
            Requires.NotNull(algorithm, "algorithm");

            if (algorithm.BlockSize == 0 && pclAlgorithm.GetName() == SymmetricAlgorithmName.Rc4)
            {
                // This is a streaming cipher without a block size. Return 1 to emulate behavior of other platforms.
                return 1;
            }

            return algorithm.BlockSize;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns a crypto key management for a specified algorithm.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>An instance of <see cref="ISymmetricKeyAlgorithmProvider"/>.</returns>
        public static ISymmetricKeyAlgorithmProvider OpenAlgorithm(this ISymmetricKeyAlgorithmProviderFactory factory, SymmetricAlgorithm algorithm)
        {
            Requires.NotNull(factory, nameof(factory));

            return factory.OpenAlgorithm(algorithm.GetName(), algorithm.GetMode(), algorithm.GetPadding());
        }