Skip to content

Commit

Permalink
add option to provide external time to encryption
Browse files Browse the repository at this point in the history
Useful for testing
  • Loading branch information
hfxbse committed Mar 25, 2024
1 parent 30ab5e4 commit 113667a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ async function fetchVerification(): Promise<{ csrf: string, keyVersion: number,
}
}

async function encryptPassword({password, keyId, publicKey}: {
async function encryptPassword({time = new Date(), password, keyId, publicKey}: {
time?: Date,
password: string,
keyId: number,
publicKey: string,
}) {
const passwordBuffer = encoder.encode(password)
const time = (Date.now() / 1000).toFixed(0);
const timeString = (time.getTime() / 1000).toFixed(0)

if (publicKey.length !== 64) throw new Error("Wrong public key hex.")
const keyBuffer = new Uint8Array(hexToArrayBuffer(publicKey))
Expand All @@ -54,7 +55,7 @@ async function encryptPassword({password, keyId, publicKey}: {

const exportedKeys = await crypto.subtle.exportKey("raw", rawKeys)
const cipher = new Uint8Array(await crypto.subtle.encrypt({
additionalData: encoder.encode(time),
additionalData: encoder.encode(timeString),
iv,
name: algorithmName,
tagLength: 16 * 8
Expand All @@ -72,7 +73,7 @@ async function encryptPassword({password, keyId, publicKey}: {
const converted = []
target.forEach(element => converted.push(String.fromCharCode(element)))

return {time, encryptedPassword: btoa(converted.join(''))}
return {time: parseInt(timeString, 10), encryptedPassword: btoa(converted.join(''))}
}

export async function login({user, password}: { user: string, password: string }): Promise<string> {
Expand Down

0 comments on commit 113667a

Please sign in to comment.