Skip to content

Commit

Permalink
Merge pull request #1206 from vivliostyle/refactor/remove-sha1
Browse files Browse the repository at this point in the history
refactor: remove "sha1.ts" and use crypto.subtle.digest() instead
  • Loading branch information
MurakamiShinyu authored Jul 12, 2023
2 parents ad87dab + 1e96a1a commit 71dd03e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 204 deletions.
35 changes: 0 additions & 35 deletions packages/core/src/vivliostyle/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,41 +571,6 @@ export function numberCompare(a: number, b: number): number {
return a - b;
}

export const base64Chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

export function appendBase64(sb: StringBuffer, data: string): void {
const length = data.length;
const length3 = Math.floor(length / 3) * 3;
for (let i = 0; i < length3; i += 3) {
const c1 = data.charCodeAt(i) & 255;
const c2 = data.charCodeAt(i + 1) & 255;
const c3 = data.charCodeAt(i + 2) & 255;
sb.append(base64Chars.charAt(c1 >> 2));
sb.append(base64Chars.charAt(((c1 << 4) | (c2 >> 4)) & 63));
sb.append(base64Chars.charAt(((c2 << 2) | (c3 >> 6)) & 63));
sb.append(base64Chars.charAt(c3 & 63));
}
switch (length - length3) {
case 1: {
const p1 = data.charCodeAt(length3) & 255;
sb.append(base64Chars.charAt(p1 >> 2));
sb.append(base64Chars.charAt((p1 << 4) & 63));
sb.append("==");
break;
}
case 2: {
const q1 = data.charCodeAt(length3) & 255;
const q2 = data.charCodeAt(length3 + 1) & 255;
sb.append(base64Chars.charAt(q1 >> 2));
sb.append(base64Chars.charAt(((q1 << 4) | (q2 >> 4)) & 63));
sb.append(base64Chars.charAt((q2 << 2) & 63));
sb.append("=");
break;
}
}
}

/**
* Index array using key function. First encountered item wins on collision.
* Elements with empty and null keys are dropped.
Expand Down
36 changes: 23 additions & 13 deletions packages/core/src/vivliostyle/epub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import * as Font from "./font";
import * as Logging from "./logging";
import * as Net from "./net";
import * as OPS from "./ops";
import * as SHA1 from "./sha1";
import * as Task from "./task";
import * as Toc from "./toc";
import * as Vgen from "./vgen";
Expand Down Expand Up @@ -469,25 +468,36 @@ export function getOPFItemId(item: OPFItem): string | null {
}

export function makeDeobfuscator(uid: string): (p1: Blob) => Task.Result<Blob> {
// TODO: use UTF8 of uid
const sha1Sum = SHA1.bytesToSHA1Int8(uid);
return (blob) => {
const frame = Task.newFrame("deobfuscator") as Task.Frame<Blob>;
const head = blob.slice(0, 1040);
const tail = blob.slice(1040, blob.size);
Net.readBlob(head).then((buf) => {
const dataView = new DataView(buf);
for (let k = 0; k < dataView.byteLength; k++) {
let b = dataView.getUint8(k);
b ^= sha1Sum[k % 20];
dataView.setUint8(k, b);
}
frame.finish(Net.makeBlob([dataView, tail]));
makeDigest("SHA-1", uid).then((hash) => {
const head = blob.slice(0, 1040);
const tail = blob.slice(1040, blob.size);
Net.readBlob(head).then((buf) => {
const dataView = new DataView(buf);
for (let k = 0; k < dataView.byteLength; k++) {
let b = dataView.getUint8(k);
b ^= hash[k % 20];
dataView.setUint8(k, b);
}
frame.finish(Net.makeBlob([dataView, tail]));
});
});
return frame.result();
};
}

function makeDigest(algorithm: string, str: string): Task.Result<Uint8Array> {
const frame = Task.newFrame("makeDigest") as Task.Frame<Uint8Array>;
const continuation = frame.suspend();
window.crypto.subtle
.digest(algorithm, new TextEncoder().encode(str))
.then((buf) => {
continuation.schedule(new Uint8Array(buf));
});
return frame.result();
}

type RawMeta = {
[key: string]: RawMetaItem[];
};
Expand Down
156 changes: 0 additions & 156 deletions packages/core/src/vivliostyle/sha1.ts

This file was deleted.

1 comment on commit 71dd03e

@vercel
Copy link

@vercel vercel bot commented on 71dd03e Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vivliostyle – ./

vivliostyle-vivliostyle.vercel.app
vivliostyle-git-master-vivliostyle.vercel.app
vivliostyle.vercel.app

Please sign in to comment.