我AES/GCM/NoPadding在Java 8中使用加密,我想知道我的代码是否存在安全漏洞.我的代码似乎有效,因为它加密和解密文本,但一些细节尚不清楚.
我的主要问题是:
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] iv = cipher.getIV(); // ?????
Run Code Online (Sandbox Code Playgroud)
IV是否满足"对于给定的密钥,IV不得重复"的要求.来自RFC 4106?
我也很感激我对相关问题的任何答案/见解(见下文),但第一个问题最让我烦恼.我不知道在哪里可以找到解决此问题的源代码或文档.
这是完整的代码,粗略.如果我在撰写这篇文章时引入了错误,我深表歉意:
class Encryptor {
Key key;
Encryptor(byte[] key) {
if (key.length != 32) throw new IllegalArgumentException();
this.key = new SecretKeySpec(key, "AES");
}
// the output is sent to users
byte[] encrypt(byte[] src) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] iv = cipher.getIV(); // See question #1
assert iv.length == 12; // See question …Run Code Online (Sandbox Code Playgroud) 当提供线程安全映射时, Guava的Tables.newCustomTable(Map,Supplier)方法是否会返回线程安全表?例如:
public static <R, C, V> Table<R, C, V> newConcurrentTable() {
return Tables.newCustomTable(
new ConcurrentHashMap<R, Map<C, V>>(),
new Supplier<Map<C, V>>() {
public Map<C, V> get() {
return new ConcurrentHashMap<C, V>();
}
});
}
Run Code Online (Sandbox Code Playgroud)
该代码是否实际返回并发表?
我试图在Dart中编写一个可以并行处理多个请求的HTTP服务器.到目前为止,我没有成功实现"并行"部分.
这是我最初尝试的内容:
import 'dart:io';
main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
server.listen((HttpRequest request) {
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
while (stopwatch.elapsedMilliseconds < 1000) { /* do nothing */ }
request.response.statusCode = HttpStatus.OK;
request.response.write(stopwatch.elapsedMilliseconds.toString());
request.response.close().catchError(print);
});
});
}
Run Code Online (Sandbox Code Playgroud)
在每个请求上它忙于工作一秒钟,然后完成.我让它以这种方式处理请求,以便它的时间可以预测,因此我可以很容易地看到Windows任务管理器中的请求的效果(CPU核心跳到100%使用率).
我可以说这不是并行处理请求,因为:
如果我将几个浏览器选项卡加载到http://example:8080/然后全部刷新,则选项卡依次加载,每个选项卡之间大约1秒钟.
如果我使用带有这些设置 的负载测试工具wrkwrk -d 10 -c 8 -t 8 http://example:8080/ ......它在我给它的10秒内完成了5到8个请求.如果服务器使用我的所有8个核心,我预计一个数字接近80个请求.
当我在wrk测试期间打开Windows任务管理器时,我发现我的核心中只有一个接近100%的使用率,其余的几乎都是空闲的.
所以,然后我尝试使用隔离,希望为每个请求手动生成一个新的隔离/线程:
import 'dart:io';
import 'dart:isolate';
main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
server.listen((HttpRequest request) {
spawnFunction(handleRequest).send(request);
});
});
}
handleRequest() {
port.receive((HttpRequest request, SendPort sender) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个这样的方法:
static boolean fitsInDouble(long x) {
// return true if x can be represented
// as a numerically-equivalent double
}
Run Code Online (Sandbox Code Playgroud)
我正在努力寻找最有效的实施方案.我选择了一个,然后一个同事运行基准测试并获得了不同的相对结果.对我而言,最快的实施并不是最快的.
这些基准测试有什么问题吗?
package rnd;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Measurement(iterations = 5)
@Warmup(iterations = 5)
public class Benchmarks {
public static void main(String[] args) throws Exception {
Options options = new …Run Code Online (Sandbox Code Playgroud) java ×3
aes-gcm ×1
cryptography ×1
dart ×1
dart-io ×1
encryption ×1
guava ×1
jmh ×1
performance ×1
security ×1