From f5c5c8b40e6f59808dec8ba99a91323547e34916 Mon Sep 17 00:00:00 2001 From: src48597962 <48597962@qq.com> Date: Mon, 6 Mar 2023 23:33:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'test.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test.js diff --git a/test.js b/test.js new file mode 100644 index 00000000..d5bd4876 --- /dev/null +++ b/test.js @@ -0,0 +1,93 @@ +function sha256(str) { + const data = new Uint8Array([...str].map(c => c.charCodeAt(0))); + return crypto.subtle.digest('SHA-256', data).then(hashBuffer => { + const hashArray = Array.from(new Uint8Array(hashBuffer)); + const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join(''); + return hashHex; + }); +} +function getRandomBytes(len) { + const arr = new Uint8Array(len); + window.crypto.getRandomValues(arr); + return Array.from(arr, (dec) => ('0' + dec.toString(16)).substr(-2)).join(''); +} +function toHex(buf) { + return Array.prototype.map.call(new Uint8Array(buf), x => ('00' + x.toString(16)).slice(-2)).join(''); +} +function fromHex(hex) { + return new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); +} +const curve = 'P-256'; +const appId = '5ddexxxbdf9e4966b387ba58f4b3fdc3'; +const deviceId = '2eexxx84b9fc4860b9427feb97a4c142'; +const userId = '3426axxxbaa04e1ea9ee01bd998d06d4'; +let nonce = 0; +const keyPairPromise = crypto.subtle.generateKey( + { + name: 'ECDSA', + namedCurve: curve + }, + true, + ['sign', 'verify'] +); +keyPairPromise.then(keyPair => { + const publicKeyPromise = crypto.subtle.exportKey('raw', keyPair.publicKey); + const privateKeyPromise = crypto.subtle.exportKey('raw', keyPair.privateKey); + Promise.all([publicKeyPromise, privateKeyPromise]).then(values => { + const publicKey = toHex(values[0]); + const privateKey = toHex(values[1]); + const r = (appId, deviceId, userId, nonce) => { + return `${appId}:${deviceId}:${userId}:${nonce}`; + }; + const sign = (appId, deviceId, userId, nonce) => { + const data = r(appId, deviceId, userId, nonce); + sha256(data).then(hash => { + crypto.subtle.sign( + { + name: 'ECDSA', + hash: {name: 'SHA-256'}, + }, + keyPair.privateKey, + fromHex(hash) + ).then(signature => { + const signatureHex = toHex(new Uint8Array(signature)) + '01'; + const headers = { + 'authorization': 'Bearer eyJhbGciOiJSUz', + 'origin': 'https://www.yundrive.com', + 'referer': 'https://www.yundrive.com/', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41', + 'x-canary': 'client=web,app=adrive,version=v3.17.0', + 'x-device-id': deviceId, + 'x-signature': signatureHex, + }; + const data = { + 'deviceName': 'Edge浏览器', + 'modelName': 'Windows网页版', + 'pubKey': publicKey, + }; + const options = { + method: 'POST', + headers: headers, + body: JSON.stringify(data), + }; + fetch('https://api.yundrive.com/session', options).then(response => { + return response.json(); + }).then(data => { + console.log(data); + }).catch(error => { + console.error(error); + }); + }).catch(error => { + console.error(error); + }); + }).catch(error => { + console.error(error); + }); + }; + sign(appId, deviceId, userId, nonce); + }).catch(error => { + console.error(error); + }); +}).catch(error => { + console.error(error); +}); \ No newline at end of file