-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto-polyfill.js
More file actions
65 lines (61 loc) · 1.69 KB
/
Copy pathcrypto-polyfill.js
File metadata and controls
65 lines (61 loc) · 1.69 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
// Crypto polyfill for React Native
import { Buffer } from '@craftzdog/react-native-buffer'
// Ultra-simple crypto implementations
export function randomBytes(size) {
console.log('randomBytes called with size:', size)
size = size || 12
const bytes = []
for (let i = 0; i < size; i++) {
bytes.push(Math.floor(Math.random() * 256))
}
return Buffer.from(bytes)
}
export function pbkdf2Sync(password, salt, iterations, keylen, digest) {
console.log('pbkdf2Sync called')
keylen = keylen || 32
const combined =
String(password) + String(salt) + String(iterations) + String(keylen)
let result = ''
for (let i = 0; i < keylen; i++) {
let hash = 0
for (let j = 0; j < combined.length; j++) {
hash = ((hash << 5) - hash + combined.charCodeAt(j) + i) & 0xffff
}
result += (hash & 0xff).toString(16).padStart(2, '0')
}
return Buffer.from(result, 'hex')
}
export function createHash(algorithm) {
console.log('createHash called')
let data = ''
return {
update: function (input) {
data += String(input)
return this
},
digest: function (encoding) {
let hash = 0
for (let i = 0; i < data.length; i++) {
hash = ((hash << 5) - hash + data.charCodeAt(i)) & 0xffffffff
}
const hex =
Math.abs(hash).toString(16).padStart(8, '0') +
Math.abs(hash * 2)
.toString(16)
.padStart(8, '0') +
Math.abs(hash * 3)
.toString(16)
.padStart(8, '0') +
Math.abs(hash * 4)
.toString(16)
.padStart(8, '0')
return encoding === 'hex' ? hex : Buffer.from(hex, 'hex')
}
}
}
// Default export
export default {
randomBytes,
pbkdf2Sync,
createHash
}