forked from EntropyString/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharSet.js
More file actions
108 lines (87 loc) · 2.65 KB
/
charSet.js
File metadata and controls
108 lines (87 loc) · 2.65 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import lcm from './lcm'
class CharSet {
constructor(chars, ndxFn) {
this.chars = chars
this.ndxFn = ndxFn
this.len = chars.length
this.entropyPerChar = Math.floor(Math.log2(this.len))
if (this.entropyPerChar != Math.log2(this.len)) {
throw new Error('EntropyString only supports CharSets with a power of 2 characters')
}
this.charsPerChunk = lcm(this.entropyPerChar, 8) / this.entropyPerChar
}
use(chars) {
const len = chars.length
// Ensure correct number of characters
if (len != this.len) {
throw new Error('Invalid character count')
}
// Ensure no repeated characters
for (let i = 0; i < len; i++) {
let c = chars.charAt(i)
for (let j = i+1; j < len; j++) {
if (c === chars.charAt(j)) {
throw new Error('Characters not unique')
}
}
}
this.chars = chars
}
}
const _ndx64 = (chunk, slice, bytes) => {
return _ndxGen(chunk, slice, bytes, 6)
}
const _ndx32 = (chunk, slice, bytes) => {
return _ndxGen(chunk, slice, bytes, 5)
}
const _ndx16 = (chunk, slice, bytes) => {
return ((bytes[chunk]<<(4*slice))&0xff)>>4
}
const _ndx8 = (chunk, slice, bytes) => {
return _ndxGen(chunk, slice, bytes, 3)
}
const _ndx4 = (chunk, slice, bytes) => {
return ((bytes[chunk]<<(2*slice))&0xff)>>6
}
const _ndx2 = (chunk, slice, bytes) => {
return ((bytes[chunk]<<slice)&0xff)>>7
}
const _ndxGen = (chunk, slice, bytes, bitsPerSlice) => {
let bitsPerByte = 8
let slicesPerChunk = lcm(bitsPerSlice, bitsPerByte) / bitsPerByte
let bNum = chunk * slicesPerChunk
let rShift = bitsPerByte - bitsPerSlice
let lOffset = Math.floor((slice*bitsPerSlice)/bitsPerByte)
let lShift = (slice*bitsPerSlice) % bitsPerByte
let ndx = ((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
let rOffset = Math.ceil((slice*bitsPerSlice)/bitsPerByte)
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*bitsPerSlice) % bitsPerByte
if (rShift < rShiftIt) {
ndx += bytes[bNum+rOffset]>>rShiftIt
}
return ndx
}
const charSet64 =
new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", _ndx64)
const charSet32 =
new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT", _ndx32)
const charSet16 =
new CharSet("0123456789abcdef", _ndx16)
const charSet8 =
new CharSet("01234567", _ndx8)
const charSet4 =
new CharSet("ATCG", _ndx4)
const charSet2 =
new CharSet("01", _ndx2)
const isValid = (charSet) => {
return charSet instanceof CharSet
}
export default {
charSet64: charSet64,
charSet32: charSet32,
charSet16: charSet16,
charSet8: charSet8,
charSet4: charSet4,
charSet2: charSet2,
isValid: isValid
}