-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib,build: use Web Crypto API in browser
PR-URL: #360 Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
- Loading branch information
Showing
3 changed files
with
38 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
/* eslint-env browser */ | ||
|
||
// Browser adapter for required functions from Node.js crypto module. | ||
const crypto = {}; | ||
|
||
crypto.randomBytes = (count) => { | ||
const buf = Buffer.alloc(count); | ||
crypto.randomFillSync(buf); | ||
return buf; | ||
}; | ||
|
||
if (window.crypto && window.crypto.getRandomValues) { | ||
crypto.randomFillSync = (buf) => { | ||
window.crypto.getRandomValues(buf); | ||
}; | ||
} else { | ||
console.warn( | ||
'Web Crypto API is not supported in your browser!', | ||
'Using Math.random() instead.' | ||
); | ||
crypto.randomFillSync = (buf) => { | ||
for (let i = 0; i < buf.length; i++) { | ||
buf[i] = Math.floor(0x100 * Math.random()); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = crypto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters