-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (47 loc) · 1.34 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
import Vue from 'vue'
import * as nacl from 'ecma-nacl'
const VueNaclCrypter = {
encodeUTF8 (str) {
var utf8 = unescape(encodeURIComponent(str))
var arr = []
for (var i = 0; i < utf8.length; i++) {
arr.push(utf8.charCodeAt(i))
}
return new Uint8Array(arr)
},
NextNon () {
var nonce = new Uint8Array(24)
crypto.getRandomValues(nonce)
return nonce
},
arr2str (arr) {
var utf8 = Array.from(arr).map(function (item) {
return String.fromCharCode(item)
}).join('')
return decodeURIComponent(escape(utf8))
},
isObject (value) {
return value && typeof value === 'object' && value.constructor === Object
},
encrypt (text, nonce = null, key) {
var ctext = this.encodeUTF8(text)
var ckey = this.encodeUTF8(key)
return nacl.secret_box.formatWN.pack(ctext, nonce || this.NextNon(), ckey)
},
decrypt (text, key) {
var res = this.isObject(text) ? Object.values(text) : text.split(',')
var dtext = new Uint8Array(res)
var dkey = this.encodeUTF8(key)
var decode = nacl.secret_box.formatWN.open(dtext, dkey)
var uncode = this.arr2str(decode)
return uncode
}
}
const Dcrypt = {}
Dcrypt.encrypt = VueNaclCrypter.encrypt
Dcrypt.decrypt = VueNaclCrypter.decrypt
export default () => {
Vue.prototype.$Dcrypt = Dcrypt
}
export { VueNaclCrypter }
export { Dcrypt }