小编Mic*_*son的帖子

Java AES/GCM/NoPadding - 什么是cipher.getIV()给我的?

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)

java security encryption cryptography aes-gcm

32
推荐指数
1
解决办法
2万
查看次数

当Guava Table线程的支持映射是线程安全时,它是否安全?

当提供线程安全映射时, 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)

该代码是否实际返回并发表?

java guava

16
推荐指数
2
解决办法
6639
查看次数

Dart:并行处理传入的HTTP请求

我试图在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%使用率).

我可以说这不是并行处理请求,因为:

  1. 如果我将几个浏览器选项卡加载到http://example:8080/然后全部刷新,则选项卡依次加载,每个选项卡之间大约1秒钟.

  2. 如果我使用带有这些设置 的负载测试工具wrkwrk -d 10 -c 8 -t 8 http://example:8080/ ......它在我给它的10秒内完成了5到8个请求.如果服务器使用我的所有8个核心,我预计一个数字接近80个请求.

  3. 当我在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)

dart dart-isolates dart-io

14
推荐指数
3
解决办法
1582
查看次数

这个JMH基准测试在机器之间是不一致的 - 为什么?

我正在尝试编写一个这样的方法:

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 performance jmh

8
推荐指数
2
解决办法
685
查看次数