<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PCLCrypto;

namespace GPSOAuthSharp
{
    internal class GoogleKeyUtils
    {
        // BitConverter has different endianness, hence the Reverse()
        public static RSAParameters KeyFromB64(string b64Key)
        {
            byte[] decoded = Convert.FromBase64String(b64Key);
            int modLength = BitConverter.ToInt32(decoded.Take(4).Reverse().ToArray(), 0);
            byte[] mod = decoded.Skip(4).Take(modLength).ToArray();
            int expLength = BitConverter.ToInt32(decoded.Skip(modLength + 4).Take(4).Reverse().ToArray(), 0);
            byte[] exponent = decoded.Skip(modLength + 8).Take(expLength).ToArray();
            var rsaKeyInfo = new RSAParameters
            {
                Modulus = mod,
                Exponent = exponent
            };
            return rsaKeyInfo;
        }

        // Python version returns a string, but we use byte[] to get the same results
        public static byte[] KeyToStruct(RSAParameters key)
        {
            byte[] modLength = { 0x00, 0x00, 0x00, 0x80 };
            byte[] mod = key.Modulus;
            byte[] expLength = { 0x00, 0x00, 0x00, 0x03 };
            byte[] exponent = key.Exponent;
            return DataTypeUtils.CombineBytes(modLength, mod, expLength, exponent);
        }

        public static Dictionary<string, string> ParseAuthResponse(string text)
        {
            var split = text.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            return split.Select(line => line.Split('=')).ToDictionary(parts => parts[0], parts => parts[1]);
        }

        public static string CreateSignature(string email, string password, RSAParameters key)
        {
            IAsymmetricKeyAlgorithmProvider alg = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1);
            var importedKey = alg.ImportParameters(key);
            var hasher = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1);
            byte[] prefix = { 0x00 };
            byte[] keyArray = KeyToStruct(key);
            byte[] something = Encoding.UTF8.GetBytes(email + "\x00" + password);
            byte[] hash = hasher.HashData(keyArray).Take(4).ToArray();
            byte[] encrypted = WinRTCrypto.CryptographicEngine.Encrypt(importedKey, something);
            byte[] combinedBytes = DataTypeUtils.CombineBytes(prefix, hash, encrypted);
            return DataTypeUtils.UrlSafeBase64(combinedBytes);
        }
    }
}<commit_msg>Fix broken urls Added changed from https://github.com/Fliperworld/GPSOAuthSharp/commit/042caa7374532568fa9ac759aac1b45952716f2c<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PCLCrypto;

namespace GPSOAuthSharp
{
    internal class GoogleKeyUtils
    {
        // BitConverter has different endianness, hence the Reverse()
        public static RSAParameters KeyFromB64(string b64Key)
        {
            byte[] decoded = Convert.FromBase64String(b64Key);
            int modLength = BitConverter.ToInt32(decoded.Take(4).Reverse().ToArray(), 0);
            byte[] mod = decoded.Skip(4).Take(modLength).ToArray();
            int expLength = BitConverter.ToInt32(decoded.Skip(modLength + 4).Take(4).Reverse().ToArray(), 0);
            byte[] exponent = decoded.Skip(modLength + 8).Take(expLength).ToArray();
            var rsaKeyInfo = new RSAParameters
            {
                Modulus = mod,
                Exponent = exponent
            };
            return rsaKeyInfo;
        }

        // Python version returns a string, but we use byte[] to get the same results
        public static byte[] KeyToStruct(RSAParameters key)
        {
            byte[] modLength = { 0x00, 0x00, 0x00, 0x80 };
            byte[] mod = key.Modulus;
            byte[] expLength = { 0x00, 0x00, 0x00, 0x03 };
            byte[] exponent = key.Exponent;
            return DataTypeUtils.CombineBytes(modLength, mod, expLength, exponent);
        }

        public static Dictionary<string, string> ParseAuthResponse(string text)
        {
            var split = text.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            return split.Select(line => line.Split(new[] {'='}, 2)).ToDictionary(parts => parts[0], parts => parts[1]);
        }

        public static string CreateSignature(string email, string password, RSAParameters key)
        {
            IAsymmetricKeyAlgorithmProvider alg = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1);
            var importedKey = alg.ImportParameters(key);
            var hasher = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1);
            byte[] prefix = { 0x00 };
            byte[] keyArray = KeyToStruct(key);
            byte[] something = Encoding.UTF8.GetBytes(email + "\x00" + password);
            byte[] hash = hasher.HashData(keyArray).Take(4).ToArray();
            byte[] encrypted = WinRTCrypto.CryptographicEngine.Encrypt(importedKey, something);
            byte[] combinedBytes = DataTypeUtils.CombineBytes(prefix, hash, encrypted);
            return DataTypeUtils.UrlSafeBase64(combinedBytes);
        }
    }
}
{'fraction_non_alphanumeric': 0.10869157124084851, 'fraction_numerical': 0.026281208935611037, 'mean_word_length': 2.585464333781965, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6488019', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1476, 'n_words': 325}