mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-08-01 02:12:01 +00:00
finish rsa implementation
This commit is contained in:
39
src/utils.js
39
src/utils.js
@@ -1,13 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const multihashing = require('multihashing')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
||||
// Check the equality of two keys
|
||||
exports.keyEqual = (k1, k2) => {
|
||||
return k1.buffer.equals(k2.buffer)
|
||||
}
|
||||
const URL = global.window && (window.URL || window.webkitURL)
|
||||
|
||||
// Hashes a key
|
||||
exports.keyHash = (key) => {
|
||||
return multihashing(key.buffer, 'sha2-256')
|
||||
exports.keyHash = (bytes) => {
|
||||
return multihashing(bytes, 'sha2-256')
|
||||
}
|
||||
|
||||
const toBlob = (content) => {
|
||||
try {
|
||||
let blob
|
||||
try {
|
||||
// BlobBuilder = Deprecated, but widely implemented
|
||||
const BlobBuilder = global.window &&
|
||||
(window.BlobBuilder ||
|
||||
window.WebKitBlobBuilder ||
|
||||
window.MozBlobBuilder ||
|
||||
window.MSBlobBuilder)
|
||||
|
||||
blob = new BlobBuilder()
|
||||
blob.append(content)
|
||||
blob = blob.getBlob()
|
||||
} catch (e) {
|
||||
// The proposed API
|
||||
blob = new window.Blob([content])
|
||||
}
|
||||
return URL.createObjectURL(blob)
|
||||
} catch (e) {
|
||||
return 'data:application/javascript,' + encodeURIComponent(content)
|
||||
}
|
||||
}
|
||||
|
||||
const rawScript = fs.readFileSync(path.join(__dirname, '../vendor/prime.worker.js'))
|
||||
|
||||
exports.workerScript = toBlob(rawScript.toString())
|
||||
|
Reference in New Issue
Block a user