Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please list alternatives #4

Open
IlyaSemenov opened this issue Mar 15, 2022 · 1 comment
Open

Please list alternatives #4

IlyaSemenov opened this issue Mar 15, 2022 · 1 comment

Comments

@IlyaSemenov
Copy link

One of my older projects uses magiccrypt. As I installed it in modern environment, It turned out that magiccrypt@2 didn't compile on Apple M1, and magiccrypt@3 wants full blown Rust environment.

Do you have any suggestions on plain JS alternatives (possibly sacrificing security/speed/etc.)? Would be ideal to list them in README.

@IlyaSemenov
Copy link
Author

FWIW, I ended up using plain crypto:

import crypto from "crypto"

import { secret_key } from "~/config"

const algorithm = "aes-256-ctr"

export function encrypt<T = any>(value: T): string {
  const cipher = crypto.createCipher(algorithm, secret_key)
  let encryped = cipher.update(JSON.stringify(value), "utf8", "hex")
  encryped += cipher.final("hex")
  return encryped
}

export function decrypt<T = any>(encrypted: string): T {
  const decipher = crypto.createDecipher(algorithm, secret_key)
  let value_json = decipher.update(encrypted, "hex", "utf8")
  value_json += decipher.final("utf8")
  return JSON.parse(value_json)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant