Esempio n. 1
0
 /// <summary>
 /// Writes a <see cref="byte"/>[] value.
 /// </summary>
 /// <param name="value">The <see cref="byte"/>[] value to write.</param>
 public override void WriteValue(byte[] value)
 {
     if (value == null)
     {
         WriteNull();
     }
     else
     {
         InternalWriteValue(JsonToken.Bytes);
         _writer.Write(_quoteChar);
         Base64Encoder.Encode(value, 0, value.Length);
         Base64Encoder.Flush();
         _writer.Write(_quoteChar);
     }
 }
Esempio n. 2
0
    public void Save()
    {
        PlayerData playerData = new PlayerData();

        foreach (IPlayerData playerDataController in playerDataVariables)
        {
            playerDataController.Save(ref playerData);
        }
        string jsonEncoded = JsonUtility.ToJson(playerData);

        FileInfo file = new FileInfo(dataPath);

        file.Directory.Create();
        File.WriteAllText(dataPath, JSON_ONLY ? jsonEncoded : Base64Encoder.Encode(jsonEncoded));
    }
        public void LineLengthTest()
        {
            String        line           = "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
            String        expectedresult = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3\r\nODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTA=";
            Base64Encoder base64         = Base64Encoder.GetInstance();
            StringReader  sr             = new StringReader(line);
            StringBuilder sb             = new StringBuilder();
            StringWriter  sw             = new StringWriter(sb);

            base64.Encode(sr, sw, System.Text.Encoding.GetEncoding("iso-8859-1"));
            log.Debug(sb.ToString());
            Assert.AreEqual(expectedresult, sb.ToString());

            Assert.AreEqual(expectedresult, base64.EncodeString(line, System.Text.Encoding.GetEncoding("iso-8859-1")));
        }
        public void Encode(byte [] data, string expected)
        {
            // Arrange
            var context = new TestCaseContext();

            var sut = new Base64Encoder(
                context.Options,
                context.Logger,
                context.StringEncoder);

            // Act
            var result = sut.Encode(data);

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 5
0
        public void Encode()
        {
            extent.LoadConfig(AppDomain.CurrentDomain.BaseDirectory + "../../extent-config.xml");
            test = extent
                   .StartTest("Encode", "An Encoding Test");

            try
            {
                Assert.True(Base64Encoder.Encode("String to encode").Equals("U3RyaW5nIHRvIGVuY29kZQ=="));
                test.Log(LogStatus.Pass, "String Encoded");
            }
            catch (Exception ex)
            {
                test.Log(LogStatus.Fail, "<pre>" + ex.StackTrace + "</pre");
            }
        }
Esempio n. 6
0
        public void Basic_encoding_to_string_with_different_input_length()
        {
            var sut = new Base64Encoder();
            var rnd = new Random(42);

            for (int i = 0; i < 15; ++i)
            {
                int    len    = 1 << i;
                byte[] source = new byte[len];
                rnd.NextBytes(source);

                string expected = Convert.ToBase64String(source);
                string actual   = sut.Encode(source);

                Assert.AreEqual(expected, actual, "Failed at length {0} (resp. i = {1})", len, i);
            }
        }
        /// <summary>
        ///   Cram-MD5 authorization.
        ///   http://tools.ietf.org/html/rfc2195
        /// </summary>
        private bool AuthenticateCramMd5(NetworkCredential credentials)
        {
            var command  = new SmtpCommand("AUTH CRAM-MD5");
            var response = _client.SendAndReceive(command);

            var base64    = response.CurrentLine.Substring(4).TrimEnd();
            var challenge = Base64Encoder.Decode(base64, Encoding.UTF8);

            var username = credentials.UserName;
            var password = credentials.Password;

            var hash = CramMd5Hasher.ComputeHash(password, challenge);

            var authentication = username + " " + hash;
            var reader         = _client.SendAndReceive(new SmtpCommand(Base64Encoder.Encode(authentication)));

            return(reader.IsOk);
        }
        public void Encode_StringOverload_FromStringEncoded()
        {
            // Arrange
            var context = new TestCaseContext();

            var sut = new Base64Encoder(
                context.Options,
                context.Logger,
                context.StringEncoder);

            var data = "123";

            // Act
            sut.Encode(data);

            // Assert
            Mock.Get(context.StringEncoder).Verify(
                m => m.EncodeFromString(data),
                Times.Once);
        }
        private bool AuthenticateCramMd5(NetworkCredential credentials)
        {
            var command  = new Pop3Command("AUTH CRAM-MD5");
            var response = _client.SendAndReceive(command);

            // don't trim the last plus !!
            var base64    = response.CurrentLine.TrimStart(Characters.Plus).Trim();
            var challenge = Base64Encoder.Decode(base64, Encoding.UTF8);

            var username = credentials.UserName;
            var password = credentials.Password;

            var hash = CramMd5Hasher.ComputeHash(password, challenge);

            var authentication = username + " " + hash;
            var authCommand    = new Pop3Command(Base64Encoder.Encode(authentication));

            _client.Send(authCommand);
            return(_client.Receive().IsPositive);
        }
Esempio n. 10
0
        public void Data_of_various_length_encoded_to_string()
        {
            var sut   = new Base64Encoder();
            var bytes = new byte[byte.MaxValue + 1];

            for (int i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = (byte)(255 - i);
            }

            for (int value = 0; value < 256; ++value)
            {
                Span <byte> source = bytes.AsSpan(0, value + 1);
                string      actual = sut.Encode(source);
#if NETCOREAPP
                string expected = Convert.ToBase64String(source);
#else
                string expected = Convert.ToBase64String(source.ToArray());
#endif
                Assert.AreEqual(expected, actual);
            }
        }
Esempio n. 11
0
        internal static string GetSignature(List <PostData> postData, string hostHeader, string secretAccessKey)
        {
            List <PostData> ordered   = GetOrderedPostData(postData);
            StringBuilder   getString = new StringBuilder();

            foreach (PostData item in ordered)
            {
                if (getString.Length != 0)
                {
                    getString.Append("&");
                }
                getString.Append(item.Field);
                getString.Append("=");
                foreach (char c in item.Value)
                {
                    if (c <= 255)
                    {
                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '-') || (c == '_') || (c == '.') || (c == '~'))
                        {
                            getString.Append(c);
                        }
                        else
                        {
                            getString.Append(string.Format("%{0:X2}", (int)c));
                        }
                    }
                }
            }

            string stringToSign = string.Format("GET{0}{1}{0}{2}{0}{3}", "\n", hostHeader, HttpRequestUri, getString);

            HMACSHA256 hmac = new HMACSHA256(Encoding.ASCII.GetBytes(secretAccessKey));

            byte[] hash      = hmac.ComputeHash(Encoding.ASCII.GetBytes(stringToSign));
            string signature = Encoding.ASCII.GetString(Base64Encoder.Encode(hash)).Replace("+", "%2B").Replace("=", "%3D");

            return(signature);
        }
Esempio n. 12
0
        /// <summary>
        ///   Login authorization, same as plain but split into two separate command/responses.
        ///   http://tools.ietf.org/html/rfc4616
        /// </summary>
        private bool AuthenticateLogin(NetworkCredential credentials)
        {
            var command  = new SmtpCommand("AUTH LOGIN");
            var response = _client.SendAndReceive(command);

            if (response.ResponseCode != 334)
            {
                return(false);
            }

            var username         = Base64Encoder.Encode(credentials.UserName);
            var userNameResponse = _client.SendAndReceive(new SmtpCommand(username));

            if (userNameResponse.ResponseCode != 334)
            {
                return(false);
            }

            var password         = Base64Encoder.Encode(credentials.Password);
            var passwordResponse = _client.SendAndReceive(new SmtpCommand(password));

            return(passwordResponse.ResponseCode == ResponseCodes.AuthenticationSuccessful);
        }
Esempio n. 13
0
 public static void ExampleBase64Encoder()
 {
     Base64Encoder.Encode(@"C:\MyFiles\1.txt", @"C:\MyFiles\1e.txt");
     Console.WriteLine(Base64Encoder.CheckCorrect(@"C:\MyFiles\1.txt", @"C:\MyFiles\1e.txt"));
 }
Esempio n. 14
0
 public char[] SerializeByteArrayMy2() => Base64Encoder.Encode(_byteArray).ToArray();
Esempio n. 15
0
 public string SerializeByteArrayMy() => new string(Base64Encoder.Encode(_byteArray).ToArray());
Esempio n. 16
0
 public void Encode_ArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => Base64Encoder.Encode(null));
 }
Esempio n. 17
0
 public void Encode_Argument0Length()
 {
     byte[] result = Base64Encoder.Encode(new byte[0]);
     Assert.IsNotNull(result);
     Assert.AreEqual(0, result.Length);
 }
Esempio n. 18
0
        public void EncodeStringTest()
        {
            var actual = Base64Encoder.Encode("ABCDEFG", Encoding.ASCII);

            Assert.Equal("QUJDREVGRw==", actual);
        }
Esempio n. 19
0
        public static string Encrypt(string plainTextPassword)
        {
            string result = Base64Encoder.Encode(plainTextPassword);

            return(result);
        }