接通 Windows 与 Web 设备间加密文字聊天
This commit is contained in:
1 parent
b04034ee89
commit
d1efdedc57
27 files changed
+880
-22
No files matched your search
@@ -7,6 +7,9 @@ interface DesktopBridge {
|
||||
apiBase: string
|
||||
signChallenge: (username: string, challenge: string) => Promise<DeviceProof>
|
||||
openBrowser?: () => Promise<void>
|
||||
signMls: (username: string, kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>
|
||||
protectMls: (key: string) => Promise<string>
|
||||
unprotectMls: (key: string) => Promise<string>
|
||||
}
|
||||
|
||||
declare global {
|
||||
@@ -53,6 +56,20 @@ async function browserKey(username: string): Promise<CryptoKeyPair> {
|
||||
|
||||
export const platform = window.xhchat ? 'windows' : 'web'
|
||||
|
||||
export function mlsBinding(kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array) {
|
||||
const prefix = new TextEncoder().encode('XHChat MLS ' + kind + ' v1\n' + user + '\n' + device + '\n')
|
||||
const bytes = new Uint8Array(prefix.length + value.length)
|
||||
bytes.set(prefix)
|
||||
bytes.set(value, prefix.length)
|
||||
return bytes
|
||||
}
|
||||
|
||||
export async function signMls(username: string, kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array<ArrayBuffer>) {
|
||||
if (window.xhchat) return window.xhchat.signMls(username, kind, user, device, value)
|
||||
const pair = await browserKey(username)
|
||||
return new Uint8Array(await crypto.subtle.sign('Ed25519', pair.privateKey, mlsBinding(kind, user, device, value)))
|
||||
}
|
||||
|
||||
export async function signChallenge(username: string, challenge: string): Promise<DeviceProof> {
|
||||
if (window.xhchat) return window.xhchat.signChallenge(username, challenge)
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user