Skip to content

Commit

Permalink
relay: add todo for verification; simplify aes
Browse files Browse the repository at this point in the history
  • Loading branch information
polymorpher committed Nov 12, 2023
1 parent 89bd463 commit a2244a6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
1 change: 1 addition & 0 deletions voice/relay/src/routes/soft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ router.get('/health', (req, res) => {
})

router.get('/key', parseDeviceToken, checkIpBan, deviceLimiter(), ipLimiter(), (req, res) => {
// TODO: validate the device token, https://developer.apple.com/documentation/devicecheck/accessing_and_modifying_per-device_data
const deviceToken = req.deviceToken
const numKeys = BigInt(OpenAIDistributedKeys.length)
const h = hexString(sha256(stringToBytes(deviceToken)))
Expand Down
13 changes: 3 additions & 10 deletions voice/relay/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'crypto'
import { SharedEncryptionIV, SharedEncryptionSecret } from './config/index.js'
import { hash as sha256 } from 'fast-sha256'

export const hexView = (bytes: Buffer | Uint8Array): string => {
return bytes && Array.from(bytes).map(x => x.toString(16).padStart(2, '0')).join('')
Expand All @@ -24,16 +25,8 @@ export function chunkstr (str: string, size: number): string[] {
return chunks
}

const aesKey = crypto
.createHash('sha512')
.update(SharedEncryptionSecret)
.digest('hex')
.substring(0, 32)
const aesIv = crypto
.createHash('sha512')
.update(SharedEncryptionIV)
.digest('hex')
.substring(0, 16)
const aesKey = sha256(stringToBytes(SharedEncryptionSecret)).slice(0, 32)
const aesIv = sha256(stringToBytes(SharedEncryptionIV)).slice(0, 16)

export function encrypt (s: string): Buffer {
const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, aesIv)
Expand Down

0 comments on commit a2244a6

Please sign in to comment.