/
file.js
49 lines (41 loc) · 976 Bytes
/
file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
/**
* @file file example
* @module autokey
* @package autokey
* @subpackage examples
* @version 0.0.1
* @author hex7c0 <hex7c0@gmail.com>
* @license GPLv3
*/
/*
* initialize module
*/
var autokey = require('..'); // use require('autokey') instead
var fs = require('fs');
var a = 'hex7c0'; // key
var b = new Buffer('ciao I\'m hex7c0\nHow are you?\n:D'); // data
var cipher = autokey(a);
var d = cipher.encodeBuffer(b); // encrypt
console.log('original:\n' + b.toString());
// use {encoding: null} when you write buffer
fs.writeFile('crypted', d, {
encoding: null
}, function(err) {
if (err) {
console.log(err);
} else {
console.log('\nencrypt:\n' + d + '\n');
}
});
// use {encoding: null} when you read buffer
fs.readFile('crypted', {
encoding: null
}, function(err, data) {
if (err) {
console.log(err);
} else {
var e = cipher.decodeBuffer(data); // decrypt
console.log('decrypt:\n' + e.toString());
}
});