Skip to content

Instantly share code, notes, and snippets.

@patrickroberts
Last active May 21, 2018 01:34
Show Gist options
  • Save patrickroberts/9fe4606118237749749c7bffe95fc066 to your computer and use it in GitHub Desktop.
Save patrickroberts/9fe4606118237749749c7bffe95fc066 to your computer and use it in GitHub Desktop.
UMD for asynchronously generating a deobfuscation effect with customizable attributes
((root, factory) => {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory)
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS.
module.exports = factory()
} else {
// Browser globals (root is window)
root.deobfuscate = factory()
}
})(typeof self !== 'undefined' ? self : this, () => {
const delay = ms => new Promise(resolve => { setTimeout(resolve, ms) })
const range = (begin, end) => [...Array(1 + end - begin).keys()].map(index => begin + index)
const toCharCode = (...string) => string.map(char => char.charCodeAt(0))
const charRange = (begin, end) => String.fromCharCode(...range(...toCharCode(begin, end)))
const choose = array => array[Math.floor(Math.random() * array.length)]
const lookup = o => key => o[key]
// Just return a value to define the module export.
return async ({
string = '',
duration = 1,
fps = 60,
random = charRange('\u0020', '\u007e'),
next = choose,
render
} = {}) => {
const { length } = string
const getters = Array(length).fill(() => choose(random))
const obfuscated = [...getters.keys()]
const timeout = duration * 1000 / length
const charAt = lookup(string)
const interval = setInterval(() => {
render(getters.map((getter, index) => getter(index)).join(''), false)
}, 1000 / fps)
let indices = Object.values(obfuscated)
while (indices.length > 0) {
await delay(timeout)
const index = next(indices)
getters[index] = charAt
delete obfuscated[index]
indices = Object.values(obfuscated)
}
clearInterval(interval)
render(string)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment