To Pool or not to Pool java crypto service providers

后端 未结 2 1356
一个人的身影
一个人的身影 2021-02-01 09:58

Solution

  • MessageDigest => create new instances as often as needed
  • KeyFactory => use a single shared instance
  • SecureRandom => use a StackObject
2条回答
  •  长发绾君心
    2021-02-01 10:14

    This test seems to be in favour of caching

    long t0 = System.currentTimeMillis();
    byte[] bytes = new byte[100];
    MessageDigest md = MessageDigest.getInstance("MD5");
    for(int i = 0; i < 1000000; i++) {
        //MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(bytes);
        md.digest();
    }
    System.out.println(System.currentTimeMillis() - t0);
    

    When md is outside the loop it prints 579, when inside - 953.

提交回复
热议问题