Replies: 4 comments 6 replies
-
If it fits, I did without +page.svelte <script>
/** @type {import('./$types').ActionData} */
export let form;
let sha256 = '';
/**
* @param {ArrayBuffer} buffer
*/
function toHex(buffer) {
return new Uint8Array(buffer).reduce(
(hex, uint) => hex + uint.toString(16).padStart(2, '0'),
''
);
}
/**
* @param {Event} event
*/
async function handleChange(event) {
const file = /** @type {HTMLInputElement} */ (event.target).files?.[0];
if (!file) return;
sha256 = toHex(await crypto.subtle.digest('SHA-256', await file.arrayBuffer()));
}
</script>
<p>SHA256 client: {sha256}</p>
<p>SHA256 server: {form?.sha256 ?? ''}</p>
<form method="POST" action="?/upload" enctype="multipart/form-data">
<input
id="csvFile"
name="csvFile"
type="file"
on:change={handleChange}
/>
<button type="submit" class="btn">Upload</button>
</form> +page.server.js /**
* @param {ArrayBuffer} buffer
*/
function toHex(buffer) {
return Buffer.from(buffer).toString('hex');
}
/** @type {import('./$types').Actions} */
export const actions = {
async upload({ request }) {
const formData = await request.formData();
const file = formData.get('csvFile');
if (!(file instanceof Blob) || file.size === 0) return { sha256: '' };
const sha256 = toHex(await crypto.subtle.digest('SHA-256', await file.arrayBuffer()));
return { sha256 };
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for the large example, but how did you set up an https server? You
need an https server to use subtle crypto.
…On Tue, 18 Oct 2022, 11:36 repsac, ***@***.***> wrote:
If it fits, I did without FileReader and node-forge and have the same
hash server-side and client-side
+page.svelte
<script>/** @type {import('./$types').ActionData} */export let form;
let sha256 = '';
/** * @param {ArrayBuffer} buffer */function toHex(buffer) {
return new Uint8Array(buffer).reduce(
(hex, uint) => hex + uint.toString(16).padStart(2, '0'),
''
);}
/** * @param {Event} event */async function handleChange(event) {
const file = /** @type {HTMLInputElement} */ (event.target).files?.[0];
if (!file) return;
sha256 = toHex(await crypto.subtle.digest('SHA-256', await file.arrayBuffer()));}</script>
<p>SHA256 client: {sha256}</p><p>SHA256 server: {form?.sha256 ?? ''}</p>
<form method="POST" action="?/upload" enctype="multipart/form-data">
<input
id="csvFile"
name="csvFile"
type="file"
on:change={handleChange}
/>
<button type="submit" class="btn">Upload</button></form>
+page.server.js
/** * @param {ArrayBuffer} buffer */function toHex(buffer) {
return Buffer.from(buffer).toString('hex');}
/** @type {import('./$types').Actions} */export const actions = {
async upload({ request }) {
const formData = await request.formData();
const file = formData.get('csvFile');
if (!(file instanceof Blob) || file.size === 0) return { sha256: '' };
const sha256 = toHex(await crypto.subtle.digest('SHA-256', await file.arrayBuffer()));
return { sha256 };
}};
—
Reply to this email directly, view it on GitHub
<#7294 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB2VD2DOMQRMGXH4JLNPUMLWDZVRPANCNFSM6AAAAAARHHKVSI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Beta Was this translation helpful? Give feedback.
5 replies
-
Would you mind sharing whether you use NginX and the setup? Thanks!
…On Tue, 18 Oct 2022, 17:12 repsac, ***@***.***> wrote:
Yes
—
Reply to this email directly, view it on GitHub
<#7294 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB2VD2BKJE6Q5WOKOWD7ISTWD247DANCNFSM6AAAAAARHHKVSI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I would like to obtain the same hash of a file on server as on the client.
I tried:
node-forge
client-sidenode-forge
I am stuck at 7.
So, first I upload a file through a form
and I compute the hash of a file in the browser with
node-forge
:This computes a hash that is the same as when I execute it from the command line with
sha256sum
, so it is reliable.However, when I upload the file and process it on SvelteKit, I am unable to obtain the same hash. I am thinking this is because the filename or something else is missing from the resulting blob compared to what is available in the browser.
I cannot use
new FileReader()
here because I am in a Node environment.How can I create the exact same hash server-side and client-side? Do I need to add the filename or something to obtain the same hash as from the command line?
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions