Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dchest committed Feb 19, 2016
0 parents commit b7102aa
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
tweetnacl-util-js
=================

String encoding utilities extracted from <https://github.com/dchest/tweetnacl-js>

Encoding/decoding functions are provided for convenience. They are correct,
however their performance and wide compatibility with uncommon runtimes is not
something that is considered important compared to the simplicity and size of
implementation. **Please use better third-party libraries if you need to.**

Installation
------------

Use a package manager:

[Bower](http://bower.io):

$ bower install tweetnacl-util

[NPM](https://www.npmjs.org/):

$ npm install tweetnacl-util

or [download source code](https://github.com/dchest/tweetnacl-util-js/releases).


Usage
------

To make keep backward compatibility with code that used `nacl.util` previously
included with TweetNaCl.js, just include it as usual:

```
<script src="nacl.min.js"></script>
<script src="nacl-util.min.js"></script>
<script>
// nacl.util functions are now available, e.g.:
// nacl.util.decodeUTF8
</script>
```

When using CommonJS:

```
var nacl = require('tweetnacl');
nacl.util = require('tweetnacl-util');
```


Documentation
-------------

#### nacl.util.decodeUTF8(string)

Decodes string and returns `Uint8Array` of bytes.

#### nacl.util.encodeUTF8(array)

Encodes `Uint8Array` or `Array` of bytes into string.

#### nacl.util.decodeBase64(string)

Decodes Base-64 encoded string and returns `Uint8Array` of bytes.

#### nacl.util.encodeBase64(array)

Encodes `Uint8Array` or `Array` of bytes into string using Base-64 encoding.
28 changes: 28 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "tweetnacl-util",
"version": "0.13.3",
"homepage": "https://dchest.github.io/tweetnacl-util-js",
"authors": [
"TweetNaCl.js Contributors"
],
"description": "String encoding utilitlies extracted from TweetNaCl.js",
"main": "nacl-util.js",
"moduleType": [
"globals",
"node"
],
"keywords": [
"base64",
"utf8",
"string",
"encoding"
],
"license": "Public domain",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"package.json"
]
}
50 changes: 50 additions & 0 deletions nacl-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Written in 2014-2016 by Dmitry Chestnykh and Devi Mandiri.
// Public domain.
(function(root, f) {
'use strict';
if (typeof module !== 'undefined' && module.exports) module.exports = f();
else if (root.nacl) root.nacl.util = f();
else {
root.nacl = {};
root.nacl.util = f();
}
}(this, function() {
'use strict';

var util = {};

util.decodeUTF8 = function(s) {
var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
};

util.encodeUTF8 = function(arr) {
var i, s = [];
for (i = 0; i < arr.length; i++) s.push(String.fromCharCode(arr[i]));
return decodeURIComponent(escape(s.join('')));
};

util.encodeBase64 = function(arr) {
if (typeof btoa === 'undefined') {
return (new Buffer(arr)).toString('base64');
} else {
var i, s = [], len = arr.length;
for (i = 0; i < len; i++) s.push(String.fromCharCode(arr[i]));
return btoa(s.join(''));
}
};

util.decodeBase64 = function(s) {
if (typeof atob === 'undefined') {
return new Uint8Array(Array.prototype.slice.call(new Buffer(s, 'base64'), 0));
} else {
var i, d = atob(s), b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
}
};

return util;

}));
1 change: 1 addition & 0 deletions nacl-util.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "tweetnacl-util",
"version": "0.13.3",
"description": "String encoding utilitlies extracted from TweetNaCl.js",
"main": "nacl-util.js",
"scripts": {
"build": "uglifyjs nacl-util.js -c -m -o nacl-util.min.js"
},
"repository": {
"type": "git",
"url": "https://github.com/dchest/tweetnacl-util-js.git"
},
"keywords": [
"base64",
"utf8",
"string",
"encoding"
],
"author": "TweetNaCl-js contributors",
"license": "Public domain",
"bugs": {
"url": "https://github.com/dchest/tweetnacl-util-js/issues"
},
"homepage": "https://github.com/dchest/tweetnacl-util-js",
"devDependencies": {
"uglify-js": "^2.6.1"
},
"browser": {
"buffer": false
}
}

0 comments on commit b7102aa

Please sign in to comment.