forked from MiniKeePass/MiniKeePass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArc4RandomStream.m
94 lines (71 loc) · 1.88 KB
/
Arc4RandomStream.m
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
//
// Arc4RandomStream.m
// KeePass2
//
// Created by Qiang Yu on 2/28/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "Arc4RandomStream.h"
#import <Security/Security.h>
@interface Arc4RandomStream (PrivateMethods)
- (void)updateState;
@end
@implementation Arc4RandomStream
- (id)init {
uint8_t buffer[256];
(void) SecRandomCopyBytes(kSecRandomDefault, sizeof(buffer), buffer);
return [self init:[NSData dataWithBytes:buffer length:sizeof(buffer)]];
}
- (id)init:(NSData *)key {
self = [super init];
if (self) {
const uint8_t *bytes = key.bytes;
NSUInteger length = key.length;
_i = 0;
_j = 0;
uint32_t index = 0;
for (uint32_t w = 0; w < 256; w++) {
_state[w] = (uint8_t)(w & 0xff);
}
int i = 0, j = 0;
uint8_t t = 0;
for (uint32_t w = 0; w < 256; w++) {
j += ((_state[w] + bytes[index]));
j &= 0xff;
t = _state[i];
_state[i] = _state[j];
_state[j] = t;
++index;
if (index >= length) {
index = 0;
}
}
[self updateState];
_index = 512; //skip first 512 bytes
}
return self;
}
- (void)updateState {
uint8_t t = 0;
for (uint32_t w = 0; w < ARC_BUFFER_SIZE; w++) {
++_i;
_i &= 0xff;
_j += _state[_i];
_j &= 0xff;
t = _state[_i];
_state[_i] = _state[_j];
_state[_j] = (uint8_t) (t & 0xff);
t = (uint8_t) (_state[_i] + _state[_j]);
_buffer[w] = _state[t & 0xff];
}
}
- (uint8_t)getByte {
uint8_t value;
if (_index == 0) {
[self updateState];
}
value = _buffer[_index];
_index = (_index + 1) & ARC_BUFFER_SIZE;
return value;
}
@end