-
Notifications
You must be signed in to change notification settings - Fork 10
/
SecureByteBuffer.java
99 lines (84 loc) · 2.86 KB
/
SecureByteBuffer.java
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
/*
* SecureString library, Obfuscated/clearable in memory string management
*
* Copyright (C) 2017-2022 Alan Evans, NovaCrypto
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Original source: https://github.com/NovaCrypto/SecureString
* You can contact the authors via github issues.
*/
package io.github.novacrypto;
import java.io.Closeable;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.Arrays;
/**
* A store of char data that is encrypted with a one-time-pad.
* Data is pinned outside of garbage collected heap.
*/
public final class SecureByteBuffer implements Closeable {
/**
* This is threadsafe, and most of the time has no contention issues.
*/
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
/**
* @param capacity maximum number of chars for buffer to store
* @return a new {@link SecureByteBuffer} instance
*/
public static SecureByteBuffer withCapacity(int capacity) {
return new SecureByteBuffer(capacity);
}
private final ByteBuffer data;
private final ByteBuffer key;
private SecureByteBuffer(int capacity) {
data = allocatePinnedBuffer(capacity);
key = allocatePinnedBuffer(capacity);
populateBufferWithSecureKeyData(key);
}
/**
* Direct buffers are outside of garbage collection.
*/
private static ByteBuffer allocatePinnedBuffer(int capacity) {
return ByteBuffer.allocateDirect(capacity);
}
private static void populateBufferWithSecureKeyData(ByteBuffer key) {
final byte[] bytes = new byte[key.capacity()];
SECURE_RANDOM.nextBytes(bytes);
key.put(bytes);
Arrays.fill(bytes, (byte) 0);
}
public SecureByteBuffer() {
this(1024);
}
public void append(byte b) {
data.put((byte) (b ^ key.get(data.position())));
}
public int length() {
return data.position();
}
public byte get(int i) {
if (i >= length())
throw new IndexOutOfBoundsException();
return (byte) (data.get(i) ^ key.get(i));
}
public int capacity() {
return data.capacity();
}
public void close() {
data.position(0);
key.position(0);
data.put(key);
}
}