Avatar
S. Ota
8721cdf007e798f80549a4bf174b973dc388e01952f0a952f5473c2cf84a7f60
A programmer. An author of nostr-keyx. Interests: Reinforcement Learning, Natural Language Processing and Artificial General Intelligence.

仕様が数字の件、理系に略語を作らせると大抵ろくでもないことになるから数字にしておいたほうがマシだと思う。

Google Authenticator や Anthy で QR コードを読み取った後、QR コードが読み取れない人用のリンクをクリックしてそこで表示されるテキスト (例: "JBSWY3DPEHPK3PXP") を安全な場所(macOS ならキーチェーンアクセス) に保存しておけば、アプリが消えても復活できます。

とにかく secret を安全な場所に保存しておけばアプリが消えても大丈夫です。

Replying to Avatar S. Ota

2要素認証のコードはこんな感じ。secret はサービス側で QR コードを表示された時に、表示するオプションがあるのでそれを押すと base32 テキストが出てきます。

```

// https://www.rfc-editor.org/rfc/rfc4226#page-6

const hotp = (secret: BinaryLike, count: number, digits: number = 6) => {

const c = Buffer.alloc(8);

c.writeBigInt64BE(BigInt(count));

const hs = createHmac('sha1', secret).update(c).digest();

const s = (hs.readUInt32BE(hs[19] & 0x0f) & 0x7fffffff);

return (s % (10 ** digits)).toString().padStart(digits, '0');

};

// https://www.rfc-editor.org/rfc/rfc6238#page-4

const totp = (secret: BinaryLike, time: number, step: number = 30, digits: number = 6, t0: number = 0) => {

const count = Math.floor((time - t0) / step);

return hotp(secret, count, digits);

};

// https://github.com/google/google-authenticator/wiki/Key-Uri-Format#secret

const twoFA = (secret: string, length: number = 1) => {

const sec = base32.decode(secret);

const now = Math.floor(Date.now() / 1000);

return { totp: Array.from({ length }, (_, i) => totp(sec, now + 30 * i, 30, 6)), time: 30 - (now % 30) };

}

```

一応未来のトークンも表示できるようにしたので30秒縛りがなくなります 😀

2要素認証のコードはこんな感じ。secret はサービス側で QR コードを表示された時に、表示するオプションがあるのでそれを押すと base32 テキストが出てきます。

```

// https://www.rfc-editor.org/rfc/rfc4226#page-6

const hotp = (secret: BinaryLike, count: number, digits: number = 6) => {

const c = Buffer.alloc(8);

c.writeBigInt64BE(BigInt(count));

const hs = createHmac('sha1', secret).update(c).digest();

const s = (hs.readUInt32BE(hs[19] & 0x0f) & 0x7fffffff);

return (s % (10 ** digits)).toString().padStart(digits, '0');

};

// https://www.rfc-editor.org/rfc/rfc6238#page-4

const totp = (secret: BinaryLike, time: number, step: number = 30, digits: number = 6, t0: number = 0) => {

const count = Math.floor((time - t0) / step);

return hotp(secret, count, digits);

};

// https://github.com/google/google-authenticator/wiki/Key-Uri-Format#secret

const twoFA = (secret: string, length: number = 1) => {

const sec = base32.decode(secret);

const now = Math.floor(Date.now() / 1000);

return { totp: Array.from({ length }, (_, i) => totp(sec, now + 30 * i, 30, 6)), time: 30 - (now % 30) };

}

```

2要素認証 は node で10行くらいで書けるのでシークレットさえちゃんと保存しておけばどうにかなることを最近知りました。

OS 標準以外だと HashiCorp Vault というのが良さそうかな?

https://qiita.com/search?q=HashiCorp+Vault

https://zenn.dev/search?q=HashiCorp%2520Vault

1. Windows において、macOS のキーチェーンアクセスに相当するのは Windows Credential Manager

2. パスワードは cmdkey.exe で作成/削除出来るが取得は出来ない

3. パスワードを取得するには PowerShell スクリプトが必要

https://support.microsoft.com/en-us/windows/accessing-credential-manager-1b5c916a-6a16-889f-8581-fc16e8165ac0

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmdkey

https://stackoverflow.com/a/65940323

JavaScript の range の書き方。

これは知らなかった。map もついでに出来てしまう。

Array.from({ length: 5 }, (_, i) => i * 2)

今まではこっちを使っていたけど、ちょっと技巧に走りすぎている感がある。

[...Array(5).keys()].map(i => i * 2)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#using_arrow_functions_and_array.from

どのリレーがどの NIP に対応しているか API で検索できた。

curl -s https://api.nostr.watch/v1/online | jq | wc -l # 336

curl -s https://api.nostr.watch/v1/nip/40 | jq | wc -l # 113

こないだ話題になっていた期限付きメッセージ(NIP-40)に対応しているのは3分の1くらい。

https://api.nostr.watch/

Misskey ちょっとみてみた。複数の Mastodon サーバを検索できるっぽい。

https://misskey.io/search?q=arxiv.org

検索 API もあった。

https://misskey.kurume-nct.com/api-doc#tag/notes/operation/notes/search

https://misskey.kurume-nct.com/api-doc#section/Usage

ちょっと情報が古いのが気になるけど Twitter API の代替候補にはなるかも。

Chrome Web Store に登録して、インストーラーの用意までしないとなかなか難しそうですね。Chrome Web Store は一度登録したことがあるので、もうすこし固まったら申請してみます。