From dfca2b51b8fbf9b690ddb1e10c2482e13a173b16 Mon Sep 17 00:00:00 2001 From: src48597962 <48597962@qq.com> Date: Tue, 7 Mar 2023 09:31:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'test.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.js | 63 ++++++++++++++++++++------------------------------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/test.js b/test.js index af1628f6..b2392d13 100644 --- a/test.js +++ b/test.js @@ -1,44 +1,25 @@ -function generateKeyPair() { - // Generate a random 256-bit private key - const privateKey = new Uint8Array(32); - crypto.getRandomValues(privateKey); - // Derive the corresponding public key - const curve = { - p: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f', - a: '0x0000000000000000000000000000000000000000000000000000000000000000', - b: '0x0000000000000000000000000000000000000000000000000000000000000007', - n: '0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', - Gx: '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', - Gy: '0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8' - }; - const ecparams = getSECCurveFromHex(curve); - const curvePt = ecparams.getG().multiply(BigInteger.fromByteArrayUnsigned(privateKey)); - const publicKeyBytes = curvePt.getEncoded(false); - const publicKeyHex = bytesToHex(publicKeyBytes); - const publicKey = '04' + publicKeyHex; - return { privateKey:privateKey, publicKey:publicKey }; +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 getSECCurveFromHex(curve) { - const p = new BigInteger(curve.p, 16); - const a = new BigInteger(curve.a, 16); - const b = new BigInteger(curve.b, 16); - const n = new BigInteger(curve.n, 16); - const Gx = new BigInteger(curve.Gx, 16); - const Gy = new BigInteger(curve.Gy, 16); - const curveParams = new ECCurveFp(p, a, b); - const curve = curveParams.getCurve(); - const generator = new ECPointFp(curve, curve.fromBigInteger(Gx), curve.fromBigInteger(Gy)); - const secp256k1 = new X9ECParameters(curveParams, generator, n); - return secp256k1.getCurve(); +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 bytesToHex(bytes) { - const hex = []; - for (let i = 0; i < bytes.length; i++) { - hex.push((bytes[i] >>> 4).toString(16)); - hex.push((bytes[i] & 0xf).toString(16)); - } - return hex.join(''); +function toHex(buf) { + return Array.prototype.map.call(new Uint8Array(buf), x => ('00' + x.toString(16)).slice(-2)).join(''); } -const keyPair = generateKeyPair(); -console.log('Private key:', keyPair.privateKey); -console.log('Public key:', keyPair.publicKey); \ No newline at end of file +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; +toast('加载完') \ No newline at end of file