kopia lustrzana https://gitlab.com/rysiekpl/libresilient
basic-integrity: cli digest kinda working
rodzic
49de3f07ec
commit
aef901fd73
|
@ -9,6 +9,23 @@
|
|||
* https://deno.land/
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* helper function, converting binary to base64
|
||||
* this need not be extremely fast, since it will only be used on digests
|
||||
*
|
||||
* binary_data - data to convert to base64
|
||||
*/
|
||||
let binToBase64 = (binary_data) => {
|
||||
return btoa(
|
||||
(new Uint8Array(binary_data))
|
||||
.reduce((bin, byte)=>{
|
||||
return bin += String.fromCharCode(byte)
|
||||
}, '')
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
let getFileIntegrity = async (path, algos) => {
|
||||
|
||||
const file = await Deno.open(
|
||||
|
@ -16,12 +33,42 @@ let getFileIntegrity = async (path, algos) => {
|
|||
{ read: true }
|
||||
);
|
||||
const fileInfo = await file.stat();
|
||||
console.log(fileInfo)
|
||||
|
||||
// are we working with a file?
|
||||
if (fileInfo.isFile) {
|
||||
const decoder = new TextDecoder();
|
||||
for await (const chunk of file.readable) {
|
||||
console.log(decoder.decode(chunk));
|
||||
console.log(`+-- reading: ${path}`)
|
||||
|
||||
// initialize
|
||||
var content = new Uint8Array()
|
||||
var buf = new Uint8Array(1000);
|
||||
|
||||
// read the first batch
|
||||
var numread = file.readSync(buf);
|
||||
|
||||
// read the rest, if there is anything to read
|
||||
while (numread !== null) {
|
||||
console.log(` +-- read: ${numread}`)
|
||||
console.log(` +-- length: ${content.length}`)
|
||||
|
||||
// there has to be a better way...
|
||||
var new_content = new Uint8Array(content.length + numread);
|
||||
console.log(` +-- new length: ${new_content.length}`)
|
||||
new_content.set(content)
|
||||
if (buf.length === numread) {
|
||||
new_content.set(buf, content.length)
|
||||
} else {
|
||||
new_content.set(buf.slice(0, numread), content.length)
|
||||
}
|
||||
content = new_content
|
||||
|
||||
// read some more
|
||||
numread = file.readSync(buf);
|
||||
}
|
||||
console.log(' +-- done.')
|
||||
|
||||
var digest = binToBase64(await crypto.subtle.digest(algos[0], content))
|
||||
console.log(digest)
|
||||
|
||||
}
|
||||
// putting this in a try-catch block as the file
|
||||
// is apparently being auto-closed?
|
||||
|
|
Ładowanie…
Reference in New Issue