Skip to content

Commit

Permalink
chore(polyfills): use cyrb64 for find uniq hashing
Browse files Browse the repository at this point in the history
should be fine to use high collisions one since non-uniq finds are rare now
  • Loading branch information
pylixonly committed May 24, 2024
1 parent e7a5067 commit 9f5a41c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
},
"dependencies": {
"fuzzysort": "^2.0.4",
"spark-md5": "^3.0.2",
"spitroast": "^1.4.4"
},
"pnpm": {
Expand Down
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions src/core/polyfills/vendettaObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { createThemedStyleSheet } from "@ui/styles";
import * as toasts from "@ui/toasts";
import { createElement, useEffect } from "react";
import { View } from "react-native";
import SparkMD5 from "spark-md5";

export async function createVdPluginObject(plugin: plugins.BunnyPlugin) {
return {
Expand All @@ -46,10 +45,10 @@ export const initVendettaObject = (): any => {
metro: {
modules: window.modules,
find: (filter: (m: any) => boolean) => {
return metro.findExports(metro.createSimpleFilter(filter, SparkMD5.hash(new Error().stack!)));
return metro.findExports(metro.createSimpleFilter(filter, cyrb64Hash(new Error().stack!)));
},
findAll: (filter: (m: any) => boolean) => {
return metro.findAllExports(metro.createSimpleFilter(filter, SparkMD5.hash(new Error().stack!)));
return metro.findAllExports(metro.createSimpleFilter(filter, cyrb64Hash(new Error().stack!)));
},
findByProps: (...props: any[]) => {
// TODO: remove this hack to fix Decor
Expand Down Expand Up @@ -229,3 +228,32 @@ export const initVendettaObject = (): any => {

return () => api.unload();
};

// cyrb53 (c) 2018 bryc (github.com/bryc). License: Public domain. Attribution appreciated.
// A fast and simple 64-bit (or 53-bit) string hash function with decent collision resistance.
// Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
// See https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript/52171480#52171480
// https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
const cyrb64 = (str: string, seed = 0) => {
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
// For a single 53-bit numeric return value we could return
// 4294967296 * (2097151 & h2) + (h1 >>> 0);
// but we instead return the full 64-bit value:
return [h2 >>> 0, h1 >>> 0];
};

// An improved, *insecure* 64-bit hash that's short, fast, and has no dependencies.
// Output is always 14 characters.
const cyrb64Hash = (str: string, seed = 0) => {
const [h2, h1] = cyrb64(str, seed);
return h2.toString(36).padStart(7, "0") + h1.toString(36).padStart(7, "0");
};

0 comments on commit 9f5a41c

Please sign in to comment.