Ejemplo n.º 1
0
        /// <summary>
        /// The getAuthKey method combines the server's public key and the client's private key using modular exponentiation to generate the server's API access key, the generated value is then hashed using SHA1
        /// </summary>
        /// <param name="privateKey">Client's private key</param>
        /// <returns>SHA1 hashed value of a server's API access key token</returns>
        public static string getAuthKey(int privateKey)
        {
            try
            {
                PublicKeyDto publicKeyDto = getServerPublicKey();

                BigInteger authKey = BigInteger.Pow(publicKeyDto.PublicKeyBase, privateKey) % publicKeyDto.PublicKeyModulo;

                string hashedAuthKeyString = EasyEncryption.SHA.ComputeSHA1Hash(authKey.ToString());

                return(hashedAuthKeyString);
            }
            catch
            {
                throw new Exception("Unable to obtain public key or generate authenication key");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The getServerPublicKey method accesses the server's API endpoint to obtain the server's public key, used for generating a token when combined with the client's private key. The token is used to access the APIs' protected resources.
        /// </summary>
        /// <returns>A PublicKeyDto object containing the server's public key elements - the base and modulo for a modular exponentiation equation</returns>
        public static PublicKeyDto getServerPublicKey()
        {
            string url = "publickeydistribution";

            HttpResponseMessage response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                PublicKeyDto publicKeyDto = response.Content.ReadAsAsync <PublicKeyDto>().Result;

                return(publicKeyDto);
            }
            else
            {
                throw new Exception("Unable to obtain public key from server");
            }
        }