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

Improved version with support for buffers #2

Open
mkg20001 opened this issue Jul 29, 2018 · 2 comments
Open

Improved version with support for buffers #2

mkg20001 opened this issue Jul 29, 2018 · 2 comments

Comments

@mkg20001
Copy link

const assert = require('assert')

function xor (data, key) {
  assert(data.length <= key.length, 'key length not equal/bigger data length')
  let out = Buffer.alloc(data.length)
  for (var i = 0; i < data.length; i++) {
    out[i] = data[i] ^ key[i]
  }
  return out
}

This improved version can encrypt buffers and use more than one-byte as encryption key

Example:

const crypto = require('crypto')
const key = crypto.randomBytes(16)
const msg = Buffer.from('hi')
const encrypted = xor(msg, key)
const decrypted = xor(encrypted, key)
console.log(String(decrypted))
@RobLoach
Copy link
Owner

This is a great idea! Mind submitting a PR for it?

@RobLoach
Copy link
Owner

Getting the following in the browser...

Uncaught ReferenceError: Buffer is not defined
    at xorCrypt (xor-crypt.js:50)
    at HTMLButtonElement.<anonymous> ((index):79)
xorCrypt @ xor-crypt.js:50
(anonymous) @ (index):79

Perhaps we could check if Buffer is available, and fallback to strings if not?

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

2 participants