Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created May 31, 2013 20:12
Show Gist options
  • Save darrelmiller/5687649 to your computer and use it in GitHub Desktop.
Save darrelmiller/5687649 to your computer and use it in GitHub Desktop.
// Ask the server for a password challenge string
var requestId = CryptographicBuffer.EncodeToHexString(CryptographicBuffer.GenerateRandom(4));
var challengeResponse = await _httpClient.GetAsync(_clientBaseUrl + "GetPasswordChallenge?requestId=" + requestId);
challengeResponse.EnsureSuccessStatusCode();
var challengeEncoded = await challengeResponse.Content.ReadAsAsync<string>();
var challengeBuffer = CryptographicBuffer.DecodeFromHexString(challengeEncoded);
// Use HMAC_SHA512 hash to encode the challenge string using the password being authenticated as the key.
var provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha512);
var passwordBuffer = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
var hmacKey = provider.CreateKey(passwordBuffer);
var buffHmac = CryptographicEngine.Sign(hmacKey, challengeBuffer);
var hmacString = CryptographicBuffer.EncodeToHexString(buffHmac);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment