Here's a quick sample of how to do bitwise encoding in javascript:
  1. var Encryption = (function() {
  2. function generateKey(seed) {
  3. var key = ""
  4. while (key.length < seed) key += Math.srand(seed)
  5. return key
  6. }
  7. /*
  8. * @param 'val' - a String to encode or a byte[] to decode.
  9. * @param 'salt' - (Optional) salt to perform bitwise encode/decode operation.
  10. */
  11. function xorHash(val, salt) {
  12. var encrypt = typeof val == 'string'
  13. var byteArr = encrypt ? val.toByteArr() : val
  14. var keyBytes = typeof salt == 'undefined' ?
  15. generateKey(byteArr.length).toByteArr() : salt.toByteArr()
  16. for (var i = 0; i < byteArr.length; i++) {
  17. byteArr[i] = byteArr[i] ^ keyBytes[i % keyBytes.length]
  18. }
  19. return (encrypt ? byteArr : String.fromCharCode.apply(String, byteArr))
  20. }
  21. return {
  22. /**
  23. *
  24. */
  25. encode : function(str, salt) {
  26. var encoded = []
  27. var bytes = xorHash(str, salt)
  28. for (var i = 0; i < bytes.length; i++) encoded.push(bytes[i].toString(16))
  29. return encoded.join(",")
  30. },
  31. /**
  32. *
  33. */
  34. decode : function(hsh, salt) {
  35. var decoded = []
  36. var hshArr = hsh.split(",")
  37. for (var i = 0; i < hshArr.length; i++) decoded.push(parseInt(hshArr[i], 16))
  38. return xorHash(decoded, salt)
  39. }
  40. }
  41. })()
  42. Math.srand = function(seed) {
  43. if (!seed) seed = new Date().getTime()
  44. seed = (seed*9301+49297) % 233280
  45. return seed/(233280.0)
  46. }
  47. String.prototype.toByteArr = function() {
  48. var arr = []
  49. for (var i = 0;i < this.length; i++) arr.push(this.charCodeAt(i))
  50. return arr
  51. }