Skip to content

Instantly share code, notes, and snippets.

@ForsakenHarmony
Last active April 15, 2016 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ForsakenHarmony/9d9e678a632bdf777602 to your computer and use it in GitHub Desktop.
Save ForsakenHarmony/9d9e678a632bdf777602 to your computer and use it in GitHub Desktop.
public string GenerateSteamGuardCodeForTime(long time) {
if(this.SharedSecret == null || this.SharedSecret.Length == 0) {
return "";
}
Console.WriteLine(time);
byte[] sharedSecretArray = Convert.FromBase64String(this.SharedSecret);
byte[] timeArray = new byte[8];
time /= 30L;
for(int i = 8; i > 0; i--) {
timeArray[i - 1] = (byte) time;
time >>= 8;
}
HMACSHA1 hmacGenerator = new HMACSHA1();
hmacGenerator.Key = sharedSecretArray;
byte[] hashedData = hmacGenerator.ComputeHash(timeArray);
byte[] codeArray = new byte[5];
try {
byte b = (byte) (hashedData[19] & 0xF);
int codePoint = (hashedData[b] & 0x7F) << 24 | (hashedData[b + 1] & 0xFF) << 16 | (hashedData[b + 2] & 0xFF) << 8 | (hashedData[b + 3] & 0xFF);
int k = 0;
for(int i = 0; i < 5; ++i) {
codeArray[i] = steamGuardCodeTranslations[codePoint % steamGuardCodeTranslations.Length];
codePoint /= steamGuardCodeTranslations.Length;
}
} catch(Exception) {
return null; //Change later, catch-alls are bad!
}
return Encoding.UTF8.GetString(codeArray);
}
generateSteamGuardCodeForTime(time) {
if (!this.sharedSecret || this.sharedSecret.length === 0) return "";
time /= 30;
let timeArray = new Uint8Array(8);
for (let i = 8; i > 0; i--) {
timeArray[i - 1] = time;
time >>= 8;
}
timeArray = timeArray.buffer;
timeArray.length = timeArray.byteLength;
let key = CryptoJS.enc.Base64.parse(this.sharedSecret);
let wordArray = CryptoJS.enc.u8array.parse(timeArray);
let hexHashedData = CryptoJS.HmacSHA1(wordArray, key).toString();
let codeArray = new Int8Array(5);
let hashedData = [];
for (let i = 0; i < hexHashedData.length / 2; i++) {
hashedData[i] = parseInt(hexHashedData.substr(i * 2, 2), 16);
}
let b = Byte.cast(hashedData[19] & 0xF);
let codePoint = (hashedData[b] & 0x7F) << 24 | (hashedData[b + 1] & 0xFF) << 16 | (hashedData[b + 2] & 0xFF) << 8 | (hashedData[b + 3] & 0xFF);
for (let i = 0; i < 5; ++i) {
codeArray[i] = this.steamGuardCodeTranslations[codePoint % this.steamGuardCodeTranslations.length];
codePoint = Math.floor(codePoint / this.steamGuardCodeTranslations.length);
}
let encodedString = String.fromCharCode.apply(void 0, codeArray);
return decodeURIComponent(escape(encodedString));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment