Skip to content

Commit

Permalink
feat: add steam_guard examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo-ds committed May 25, 2023
1 parent 5e0f106 commit 2c74fe0
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 11 deletions.
12 changes: 11 additions & 1 deletion examples/deno/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init, wasm_data, totp, hotp } from 'totp-wasm'
import { init, wasm_data, totp, hotp, steam_guard } from 'totp-wasm'
import { assertEquals, base64Decode } from './deps.ts'

await init(base64Decode(wasm_data))
Expand All @@ -25,3 +25,13 @@ Deno.test({
assertEquals(code, 473526)
},
})

Deno.test({
name: 'Steam Guard test',
fn() {
const secret = 'GM4VC2CQN5UGS33ZJJVWYUSFMQ4HOQJW'
const t = BigInt(1662681600)
const code = steam_guard(secret, t)
assertEquals(code, '4PRPM')
},
})
8 changes: 7 additions & 1 deletion examples/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init, totp, hotp } from '../../packages/totp-wasm/index'
import { init, totp, hotp, steam_guard } from '../../packages/totp-wasm/index'
import { wasm_data } from '../../packages/totp-wasm/dist/wasm_data'

test()
Expand All @@ -21,4 +21,10 @@ async function test() {
const code = totp(secret, t, digit, period)
console.log(code === 473526)
}
{
const secret = 'GM4VC2CQN5UGS33ZJJVWYUSFMQ4HOQJW'
const t = BigInt(1662681600)
const code = steam_guard(secret, t)
console.log(code === '4PRPM')
}
}
Binary file modified packages/totp-wasm/dist/totp-wasm.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/totp-wasm/dist/wasm_data.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/totp-wasm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions packages/totp-wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ export function totp(secret: string, t: bigint, digit: number, period: number):
return code
}

export function steam_guard(secret: string, t: bigint): string {
if (!instance) return ''
const str = to_cstr(instance, memory, secret)
const steam_guard = instance.exports.steam_guard as steam_guard_func
const output_ptr = steam_guard(str.ptr, str.len, t)
const code = new TextDecoder().decode(new Uint8Array(memory.buffer, output_ptr, 5))
free(instance, str.ptr)
free(instance, output_ptr)
return code
}

interface hotp_func {
(ptr: number, len: number, counter: bigint, digit: number): number
}
interface totp_func {
(ptr: number, len: number, t: bigint, digit: number, period: number): number
}

interface steam_guard_func {
(ptr: number, len: number, t: bigint): number
}
2 changes: 1 addition & 1 deletion packages/totp-wasm/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//@ts-nocheck
export { hotp, totp, init } from './index.js'
export * from './index.js'
export { wasm_data } from './dist/wasm_data.js'
8 changes: 5 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export fn totp(secret_ptr: [*]const u8, secret_len: usize, t: i64, digit: u32, p
return otp.totp(secret_ptr[0..secret_len], t, digit, period) catch return 0;
}

export fn steam_guard(secret_ptr: [*]const u8, secret_len: usize, t: i64) u32 {
_ = otp.steam_guard(secret_ptr[0..secret_len], t) catch return 0;
return 0;
export fn steam_guard(secret_ptr: [*]const u8, secret_len: usize, t: i64) [*]u8 {
var code = otp.steam_guard(secret_ptr[0..secret_len], t) catch @panic("steam_guard: steam_guard");
const output: []u8 = allocator.alloc(u8, 5) catch @panic("steam_guard: allocator.alloc");
@memcpy(output[0..5], code[0..5]);
return output[0..5 :0].ptr;
}

export fn malloc(size: usize) ?[*]const u8 {
Expand Down
6 changes: 3 additions & 3 deletions src/otp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test "totp test" {

const STEAM_CHARS: *const [26:0]u8 = "23456789BCDFGHJKMNPQRTVWXY";

pub fn steam_guard(secret: []const u8, t: i64) ![]u8 {
pub fn steam_guard(secret: []const u8, t: i64) ![5]u8 {
const alloc = std.heap.page_allocator;
var counter = @intCast(u64, @divFloor(t, 30));
var key = try base32.decode(alloc, secret);
Expand Down Expand Up @@ -96,13 +96,13 @@ pub fn steam_guard(secret: []const u8, t: i64) ![]u8 {
bin_code[i] = STEAM_CHARS[(fc % STEAM_CHARS.len)];
fc /= @intCast(u32, STEAM_CHARS.len);
}
return bin_code[0..];
return bin_code;
}

test "Steam Guard test" {
const secret: []const u8 = "GM4VC2CQN5UGS33ZJJVWYUSFMQ4HOQJW";
const t: i64 = 1662681600;
const code = "4PRPM";

try testing.expectEqualSlices(u8, code[0..], try steam_guard(secret, t));
try testing.expectEqualSlices(u8, code[0..], (try steam_guard(secret, t))[0..]);
}

0 comments on commit 2c74fe0

Please sign in to comment.