Esempio n. 1
0
        protected override void ProcessRecordInEH()
        {
            string encodingName = EncodingName;

            if (string.IsNullOrWhiteSpace(encodingName))
            {
                encodingName = EncodingFactory.EncodingNames.UTF8;
            }

            Encoding encoding = EncodingFactory.Get(encodingName);

            if (encoding == null)
            {
                throw new PSArgumentException(string.Format("Unsupported encoding: {0}", encodingName));
            }

            HashAlgorithm algorithm = HashAlgorithmFactory.Create(Algorithm);

            if (algorithm == null)
            {
                throw new PSArgumentException(string.Format("Unsupported algorithm: {0}", Algorithm));
            }

            byte[] hashBuffer = HashGenerator.ComputeStringHash(InputObject, algorithm, encoding);

            HashResult result = new HashResult()
            {
                Algorithm  = Algorithm,
                Hash       = hashBuffer.ToHex(),
                HashBuffer = hashBuffer,
            };

            WriteObject(result);
        }
Esempio n. 2
0
        protected override void ProcessRecordInEH()
        {
            string encodingName = EncodingName;

            if (string.IsNullOrWhiteSpace(encodingName))
            {
                encodingName = EncodingFactory.EncodingNames.UTF8;
            }

            Encoding encoding = EncodingFactory.Get(encodingName);

            if (encoding == null)
            {
                throw new PSArgumentException(string.Format("Unsupported encoding: {0}", encodingName));
            }

            string encodedString = Base64.EncodeString(InputObject, encoding);

            WriteObject(encodedString);
        }
Esempio n. 3
0
        private byte[] ConvertObjectToBytes(object baseObject)
        {
            // Byte
            if (baseObject is byte)
            {
                return(new byte[] { (byte)baseObject });
            }
            else if (baseObject is byte[])
            {
                return(baseObject as byte[]);
            }

            // Int16
            else if (baseObject is Int16)
            {
                return(BitConverter.GetBytes((Int16)baseObject));
            }
            else if (baseObject is Int16[])
            {
                List <byte> bytes = new List <byte>();
                foreach (var value in baseObject as Int16[])
                {
                    byte[] valueBytes = BitConverter.GetBytes(value);
                    bytes.AddRange(valueBytes);
                }
                return(bytes.ToArray());
            }

            // UInt16
            else if (baseObject is UInt16)
            {
                return(BitConverter.GetBytes((UInt16)baseObject));
            }
            else if (baseObject is UInt16[])
            {
                List <byte> bytes = new List <byte>();
                foreach (var value in baseObject as UInt16[])
                {
                    byte[] valueBytes = BitConverter.GetBytes(value);
                    bytes.AddRange(valueBytes);
                }
                return(bytes.ToArray());
            }

            // Int32
            else if (baseObject is Int32)
            {
                return(BitConverter.GetBytes((Int32)baseObject));
            }
            else if (baseObject is Int32[])
            {
                List <byte> bytes = new List <byte>();
                foreach (var value in baseObject as Int32[])
                {
                    byte[] valueBytes = BitConverter.GetBytes(value);
                    bytes.AddRange(valueBytes);
                }
                return(bytes.ToArray());
            }

            // UInt32
            else if (baseObject is UInt32)
            {
                return(BitConverter.GetBytes((UInt32)baseObject));
            }
            else if (baseObject is UInt32[])
            {
                List <byte> bytes = new List <byte>();
                foreach (var value in baseObject as UInt32[])
                {
                    byte[] valueBytes = BitConverter.GetBytes(value);
                    bytes.AddRange(valueBytes);
                }
                return(bytes.ToArray());
            }

            // Int64
            else if (baseObject is Int64)
            {
                return(BitConverter.GetBytes((Int64)baseObject));
            }
            else if (baseObject is Int64[])
            {
                List <byte> bytes = new List <byte>();
                foreach (var value in baseObject as Int64[])
                {
                    byte[] valueBytes = BitConverter.GetBytes(value);
                    bytes.AddRange(valueBytes);
                }
                return(bytes.ToArray());
            }

            // UInt64
            else if (baseObject is UInt64)
            {
                return(BitConverter.GetBytes((UInt64)baseObject));
            }
            else if (baseObject is UInt64[])
            {
                List <byte> bytes = new List <byte>();
                foreach (var value in baseObject as UInt64[])
                {
                    byte[] valueBytes = BitConverter.GetBytes(value);
                    bytes.AddRange(valueBytes);
                }
                return(bytes.ToArray());
            }

            // String
            else if (baseObject is string)
            {
                Encoding encoding = EncodingFactory.Get(Encoding);
                if (encoding == null)
                {
                    throw new PSArgumentException(string.Format("Unsupported encoding type: {0}.", Encoding));
                }

                return(encoding.GetBytes(baseObject as string));
            }

            throw new PSArgumentException(string.Format("Unsupported object type: {0}", baseObject.GetType().FullName));
        }