gpt4 book ai didi

freenet.crypt.Yarrow类的使用及代码示例

转载 作者:知者 更新时间:2024-03-20 17:18:31 28 4
gpt4 key购买 nike

本文整理了Java中freenet.crypt.Yarrow类的一些代码示例,展示了Yarrow类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Yarrow类的具体详情如下:
包路径:freenet.crypt.Yarrow
类名称:Yarrow

Yarrow介绍

[英]An implementation of the Yarrow PRNG in Java.

This class implements Yarrow-160, a cryptraphically secure PRNG developed by John Kelsey, Bruce Schneier, and Neils Ferguson. It was designed to follow the specification (www.counterpane.com/labs) given in the paper by the same authors, with the following exceptions:

  • Instead of 3DES as the output cipher, Rijndael was chosen. It was my belief that an AES candidate should be selected. Twofish was an alternate choice, but the AES implementation does not allow easy selection of a faster key-schedule, so twofish's severely impaired performance.
  • h prime, described as a 'size adaptor' was not used, since its function is only to constrain the size of a byte array, our own key generation routine was used instead (See freenet.crypt.Util#makeKey)
  • Our own entropy estimation routines are used, as they use a third-order delta calculation that is quite conservative. Still, its used along side the global multiplier and program- supplied guesses, as suggested.
    [中]Yarrow PRNG在Java中的实现。
    这个类实现了Yarrow-160,这是一种加密安全的PRNG,由John Kelsey、Bruce Schneier和Neils Ferguson开发。其设计遵循同一作者在论文中给出的规范(www.counterpane.com/labs),但以下情况除外:
    *选择Rijndael而不是3DES作为输出密码。我认为AES的候选人应该被选中。Twofish是另一种选择,但AES实现不允许轻松选择更快的密钥调度,因此Twofish的性能严重受损。
    *h prime被称为“大小适配器”,但没有使用,因为它的功能只是限制字节数组的大小,所以使用了我们自己的密钥生成例程(参见freenet.crypt.Util#makeKey)
    *我们使用自己的熵估计例程,因为它们使用非常保守的三阶增量计算。不过,正如建议的那样,它与全局乘数和程序提供的猜测一起使用。

代码示例

代码示例来源:origin: freenet/fred

@Override
public int acceptEntropy(EntropySource source, long data, int entropyGuess) {
  return acceptEntropy(source, data, entropyGuess, 1.0);
}

代码示例来源:origin: freenet/fred

@Override
public int acceptTimerEntropy(EntropySource timer) {
  return acceptTimerEntropy(timer, 1.0);
}

代码示例来源:origin: freenet/fred

private int acceptEntropy(
  EntropySource source,
  long data,
  int entropyGuess,
  double bias) {
  return accept_entropy(
    data,
    source,
    (int) (bias * Math.min(
    32,
    Math.min(estimateEntropy(source, data), entropyGuess))));
}

代码示例来源:origin: freenet/fred

private void generateOutput() {
  counterInc();
  output_buffer = new byte[counter.length];
  cipher_ctx.encipher(counter, output_buffer);
  if(output_count++ > Pg) {
    output_count = 0;
    nextBytes(tmp);
    rekey(tmp);
  }
}

代码示例来源:origin: freenet/fred

private void entropy_init(File seed, boolean reseedOnStartup) {
  if(reseedOnStartup) {
    Properties sys = System.getProperties();
    EntropySource startupEntropy = new EntropySource();
    // Consume the system properties list
    for(Enumeration<?> enu = sys.propertyNames(); enu.hasMoreElements();) {
      String key = (String) enu.nextElement();
      consumeString(key);
      consumeString(sys.getProperty(key));
    }
    // Consume the local IP address
    try {
      consumeString(InetAddress.getLocalHost().toString());
    } catch(Exception e) {
      // Ignore
    }
    readStartupEntropy(startupEntropy);
  }
  read_seed(seed);
}

代码示例来源:origin: freenet/fred

Yarrow r = new Yarrow(new File("/dev/urandom"), "SHA1", "Rijndael", true, false);
  long start = System.currentTimeMillis();
  for(int i = 0; i < 100; i++)
    r.nextBytes(b);
  System.out.println(
    (double) (System.currentTimeMillis() - start) / (100 * b.length) * 1024 + " ms/k");
  start = System.currentTimeMillis();
  for(int i = 0; i < 1000; i++)
    r.nextInt();
  System.out.println(
    (double) (System.currentTimeMillis() - start) / 1000 + " ms/int");
  start = System.currentTimeMillis();
  for(int i = 0; i < 1000; i++)
    r.nextLong();
  System.out.println(
    (double) (System.currentTimeMillis() - start) / 1000 + " ms/long");
  int kb = Integer.parseInt(args[1]);
  for(int i = 0; i < kb; i++) {
    r.nextBytes(b);
    System.out.write(b);
  long start = System.currentTimeMillis();
  for(int i = 0; i < 100000; i++)
    r.acceptEntropy(t, System.currentTimeMillis(), 32);
  System.err.println(
    (double) (System.currentTimeMillis() - start) / 100000);

代码示例来源:origin: freenet/fred

accumulator_init(digest);
  reseed_init(digest);
  generator_init(cipher);
} catch(NoSuchAlgorithmException e) {
  Logger.error(this, "Could not init pools trying to getInstance(" + digest + "): " + e, e);
  seedfile = null;
if(reseedOnStartup) {
  entropy_init(seed, reseedOnStartup);
  seedFromExternalStuff(canBlock);
  fast_pool_reseed();
  slow_pool_reseed();
} else {
  read_seed(seed);

代码示例来源:origin: freenet/fred

public void testDouble() {
  Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false, false, false);
  ScalarSampleStatistics sample = new ScalarSampleStatistics();
  for(int i = 0; i < 10000; ++i) {
    sample.add(y.nextDouble());
  }
  assertEquals(0.5, sample.getMean(), 0.02);
  assertEquals(1.0 / (2.0 * Math.sqrt(3.0)), sample.getStandardDeviation(), 0.002);
}

代码示例来源:origin: freenet/fred

public void testNextBoolean() {
    Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false, false, false);
    int[] results = new int[2];
    int RUNS = 1000000;
    for(int i=0; i<RUNS; i++) {
      if(y.nextBoolean())
        results[0]++;
      else
        results[1]++;
    }

    assertEquals(RUNS, results[0]+results[1]);
    assertTrue(results[0] > RUNS/2 - RUNS/1000);
    assertTrue(results[1] > RUNS/2 - RUNS/1000);
  }
}

代码示例来源:origin: freenet/fred

RandomSource random = randomSource != null ? randomSource : new Yarrow();

代码示例来源:origin: freenet/fred

/**
 * Seed handling
 */
private void read_seed(File filename) {
  FileInputStream fis = null;
  BufferedInputStream bis = null;
  DataInputStream dis = null;
  try {
    fis = new FileInputStream(filename);
    bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);
    EntropySource seedFile = new EntropySource();
      for(int i = 0; i < 32; i++)
        acceptEntropy(seedFile, dis.readLong(), 64);
    dis.close();
  } catch(EOFException f) {
    // Okay.
  } catch(IOException e) {
    Logger.error(this, "IOE trying to read the seedfile from disk : " + e.getMessage());
  } finally {
    Closer.close(dis);
    Closer.close(bis);
    Closer.close(fis);
  }
  fast_pool_reseed();
}
private long timeLastWroteSeed = -1;

代码示例来源:origin: freenet/fred

dis = new DataInputStream(fis);
      dis.readFully(buf);
      consumeBytes(buf);
      dis.readFully(buf);
      consumeBytes(buf);
      dis.close();
    } catch(Throwable t) {
    dis = new DataInputStream(fis);
    dis.readFully(buf);
    consumeBytes(buf);
    dis.readFully(buf);
    consumeBytes(buf);
  } catch(Throwable t) {
    Logger.normal(this, "Can't read /dev/urandom: " + t, t);
      dis = new DataInputStream(fis);
      dis.readFully(buf);
      consumeBytes(buf);
      dis.readFully(buf);
      consumeBytes(buf);
    } catch(Throwable t) {
      Logger.normal(this, "Can't read /dev/random: " + t, t);
  consumeBytes(buf);
  buf = sr.generateSeed(32);
  consumeBytes(buf);
consumeString(Long.toHexString(Runtime.getRuntime().freeMemory()));

代码示例来源:origin: freenet/fred

private void consumeString(String str) {
  byte[] b;
  try {
    b = str.getBytes("UTF-8");
  } catch(UnsupportedEncodingException e) {
    throw new Error("Impossible: JVM doesn't support UTF-8: " + e, e);
  }
  consumeBytes(b);
}

代码示例来源:origin: freenet/fred

entropyGatheringThread.start();
this.random = new Yarrow(seed);

代码示例来源:origin: freenet/fred

@Override
public int acceptTimerEntropy(EntropySource timer, double bias) {
  long now = System.currentTimeMillis();
  return acceptEntropy(timer, now - timer.lastVal, 32, bias);
}

代码示例来源:origin: freenet/fred

protected void readStartupEntropy(EntropySource startupEntropy) {
  // Consume the current time
  acceptEntropy(startupEntropy, System.currentTimeMillis(), 0);
  acceptEntropy(startupEntropy, System.nanoTime(), 0);
  // Free memory
  acceptEntropy(startupEntropy, Runtime.getRuntime().freeMemory(), 0);
  // Total memory
  acceptEntropy(startupEntropy, Runtime.getRuntime().totalMemory(), 0);
}

代码示例来源:origin: freenet/fred

@Override
public int acceptEntropyBytes(EntropySource source, byte[] buf, int offset,
  int length, double bias) {
  int totalRealEntropy = 0;
  for(int i = 0; i < length; i += 8) {
    long thingy = 0;
    int bytes = 0;
    for(int j = 0; j < Math.min(length, i + 8); j++) {
      thingy = (thingy << 8) + (buf[j] & 0xFF);
      bytes++;
    }
    totalRealEntropy += acceptEntropy(source, thingy, bytes * 8, bias);
  }
  return totalRealEntropy;
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com