major refactor to use shared code from thjs, and is temporarily using pure-javascript crypto