SymmetryCipher构造器

@param algorithms 算法，当前仅支持AES和DES
@param seed       随机数种子
@param keySize    keySize
@return SymmetryCipher
332866
public static CipherUtil buildInstance(Algorithms algorithms, String seed, int keySize) {
        String id = (algorithms.name() + seed + keySize).intern();
        SecretKey key = KEY_CACHE.compute(id, (k, v) -> {
            if (v == null) {
                return KeyTools.buildKey(algorithms, seed, keySize);
            } else {
                return v;
            }
        });
        return buildInstance(algorithms, key.getEncoded());
    }
413899
public static SecretKey generateDESKey(String algorithm, byte[] key) {
		if (StrUtil.isBlank(algorithm) || false == algorithm.startsWith("DES")) {
			throw new CryptoException("Algorithm [{}] is not a DES algorithm!");
		}

		SecretKey secretKey = null;
		if (null == key) {
			secretKey = generateKey(algorithm);
		} else {
			KeySpec keySpec;
			try {
				if (algorithm.startsWith("DESede")) {
					// DESede兼容
					keySpec = new DESedeKeySpec(key);
				} else {
					keySpec = new DESKeySpec(key);
				}
			} catch (InvalidKeyException e) {
				throw new CryptoException(e);
			}
			secretKey = generateKey(algorithm, keySpec);
		}
		return secretKey;
	}
481269
public static SecretKey buildKey(AbstractCipher.Algorithms algorithm, byte[] keySpec) {
        SecretKeySpec key = new SecretKeySpec(keySpec, algorithm.name());
        return key;
    }
1362794
public static byte[] sharedSecret(byte[] publicKey, byte[] privateKey) {
    final byte[] s = new byte[KEY_LEN];
    X25519.scalarMult(privateKey, 0, publicKey, 0, s, 0);
    final byte[] k = new byte[KEY_LEN];
    HSalsa20.hsalsa20(k, HSALSA20_SEED, s);
    return k;
  }
1081207
public static byte[] generateAesKey(final int keysize) {
		try {
			KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_NAME);
			keyGenerator.init(keysize);
			SecretKey secretKey = keyGenerator.generateKey();
			return secretKey.getEncoded();
		} catch (NoSuchAlgorithmException e) {
			throw new ImpossibleException(e);
		}
	}
1025117
public static byte[] generateAesKey(int keysize) {
		try {
			KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALG);
			keyGenerator.init(keysize);
			SecretKey secretKey = keyGenerator.generateKey();
			return secretKey.getEncoded();
		} catch (GeneralSecurityException e) {
			throw ExceptionUtil.unchecked(e);
		}
	}
415903
public static long hash(final long in, final long seed) {
    long hash = seed + P5;
    hash += 8;
    long k1 = in;
    k1 *= P2;
    k1 = Long.rotateLeft(k1, 31);
    k1 *= P1;
    hash ^= k1;
    hash = (Long.rotateLeft(hash, 27) * P1) + P4;
    return finalize(hash);
  }
1167984
public static SecretKey generateKey(String algorithm, KeySpec keySpec) {
		final SecretKeyFactory keyFactory = getSecretKeyFactory(algorithm);
		try {
			return keyFactory.generateSecret(keySpec);
		} catch (InvalidKeySpecException e) {
			throw new CryptoException(e);
		}
	}
1324333
public static RSAKeyString keyGen(int keySize) {
        try {
            RSAKey keys = RSA.keyGen(keySize);
            return new RSAKeyString(
                    Base64Util.encode2String(keys.getPublicKey().getEncoded()),
                    Base64Util.encode2String(keys.getPrivateKey().getEncoded())
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
509139
public static SecretKey deserializeSecretKey(byte[] keyData, String algorithm) {
        LOGGER.trace("deserialize secret key from data using algorithm \"{}\"", algorithm);
        return new SecretKeySpec(keyData, algorithm);
    }
170131
public static HMac hmac(HmacAlgorithm algorithm, String key) {
		return new HMac(algorithm, StrUtil.utf8Bytes(key));
	}
598821
private SessionTicketKey deriveKeyFromSeed(String seed) {
        byte[] seedBin = decodeHex(seed);
        byte[] keyName = hkdf(seedBin, salt, NAME_BYTES, SessionTicketKey.NAME_SIZE);
        byte[] aesKey = hkdf(seedBin, salt, AES_BYTES, SessionTicketKey.AES_KEY_SIZE);
        byte[] hmacKey = hkdf(seedBin, salt, HMAC_BYTES, SessionTicketKey.HMAC_KEY_SIZE);
        return new SessionTicketKey(keyName, hmacKey, aesKey);
    }
329548
public static SecretKey generateKey(String algorithm, byte[] key) {
		Assert.notBlank(algorithm, "Algorithm is blank!");
		SecretKey secretKey = null;
		if (algorithm.startsWith("PBE")) {
			// PBE密钥
			secretKey = generatePBEKey(algorithm, (null == key) ? null : StrUtil.str(key, CharsetUtil.CHARSET_UTF_8).toCharArray());
		} else if (algorithm.startsWith("DES")) {
			// DES密钥
			secretKey = generateDESKey(algorithm, key);
		} else {
			// 其它算法密钥
			secretKey = (null == key) ? generateKey(algorithm) : new SecretKeySpec(key, algorithm);
		}
		return secretKey;
	}
1041034
public static MessageDigest createMessageDigest(String algorithm) {
		final Provider provider = GlobalBouncyCastleProvider.INSTANCE.getProvider();

		MessageDigest messageDigest;
		try {
			messageDigest = (null == provider) ? MessageDigest.getInstance(algorithm) : MessageDigest.getInstance(algorithm, provider);
		} catch (NoSuchAlgorithmException e) {
			throw new CryptoException(e);
		}

		return messageDigest;
	}
446753
public static Random getRNG(long... seed) {
    switch(_rngType) {
    case JavaRNG:      return new H2ORandomRNG(seed[0]);
    case XorShiftRNG:  return new XorShiftRNG (seed[0]);
    case PCGRNG:       return new PCGRNG      (seed[0],seed.length > 1 ? seed[1] : 1);
    case MersenneTwisterRNG:
      // Do not copy the seeds - use them, and initialize the first two ints by
      // seeds based given argument.  The call is locked, and also
      // MersenneTwisterRNG will just copy the seeds into its datastructures
      return new MersenneTwisterRNG(ArrayUtils.unpackInts(seed));
    }
    throw H2O.fail();
  }
1274765
public static final KeyPair createKeyPair(AlgorithmParameterSpec spec) throws GeneralSecurityException {
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
	    keyGen.initialize(spec, new SecureRandom());
		return keyGen.generateKeyPair();
	}
528900
public static PrivateKey generatePrivateKey(String algorithm, KeySpec keySpec) {
		return KeyUtil.generatePrivateKey(algorithm, keySpec);
	}