-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use web api crypto instead of node:crypto for targeting evaluation #25
Merged
Conversation
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
something like below would possibly make the code compatible with browser and nodejs: async function sha256(message) {
let crypto;
// Check for browser environment
if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) {
crypto = window.crypto;
}
// Check for Node.js environment
else if (typeof global !== 'undefined' && global.crypto) {
crypto = global.crypto;
}
// Fallback to native Node.js crypto module
else {
crypto = require('crypto'); // maybe wrap with try-catch in case of uncovered runtimes... or you maybe want to fail the program because there's no way to calc hash then
}
// In the browser, use crypto.subtle.digest
if (crypto.subtle) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
// In Node.js, use the crypto module's hash function
else {
return crypto.createHash('sha256').update(message).digest('hex');
}
}
// Example usage:
sha256('Hello, world!').then(hash => {
console.log('SHA-256 hash:', hash);
}); |
Eskibear
approved these changes
Aug 21, 2024
Eskibear
reviewed
Aug 21, 2024
linglingye001
approved these changes
Aug 22, 2024
Eskibear
reviewed
Aug 22, 2024
Eskibear
reviewed
Aug 22, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why this PR?
node:crypto is not recognizable in browser environment.
Visible changes
The evaluate method of targeting filter becomes async because
crypto.subtle.digest
is async.Add testcase for browser scenario. Use playwright to simulate browser and build the package to umd style so that user can consume the package through