parent
d6fcdd853c
commit
dfca2b51b8
@ -1,44 +1,25 @@ |
|||||||
function generateKeyPair() { |
function sha256(str) { |
||||||
// Generate a random 256-bit private key
|
const data = new Uint8Array([...str].map(c => c.charCodeAt(0))); |
||||||
const privateKey = new Uint8Array(32); |
return crypto.subtle.digest('SHA-256', data).then(hashBuffer => { |
||||||
crypto.getRandomValues(privateKey); |
const hashArray = Array.from(new Uint8Array(hashBuffer)); |
||||||
// Derive the corresponding public key
|
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join(''); |
||||||
const curve = { |
return hashHex; |
||||||
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 getSECCurveFromHex(curve) { |
function getRandomBytes(len) { |
||||||
const p = new BigInteger(curve.p, 16); |
const arr = new Uint8Array(len); |
||||||
const a = new BigInteger(curve.a, 16); |
window.crypto.getRandomValues(arr); |
||||||
const b = new BigInteger(curve.b, 16); |
return Array.from(arr, (dec) => ('0' + dec.toString(16)).substr(-2)).join(''); |
||||||
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 bytesToHex(bytes) { |
function toHex(buf) { |
||||||
const hex = []; |
return Array.prototype.map.call(new Uint8Array(buf), x => ('00' + x.toString(16)).slice(-2)).join(''); |
||||||
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(''); |
|
||||||
} |
} |
||||||
const keyPair = generateKeyPair(); |
function fromHex(hex) { |
||||||
console.log('Private key:', keyPair.privateKey); |
return new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); |
||||||
console.log('Public key:', keyPair.publicKey); |
} |
||||||
|
const curve = 'P-256'; |
||||||
|
const appId = '5ddexxxbdf9e4966b387ba58f4b3fdc3'; |
||||||
|
const deviceId = '2eexxx84b9fc4860b9427feb97a4c142'; |
||||||
|
const userId = '3426axxxbaa04e1ea9ee01bd998d06d4'; |
||||||
|
let nonce = 0; |
||||||
|
toast('加载完') |
Loading…
Reference in new issue