本文整理汇总了Java中at.favre.lib.bytes.Bytes类的典型用法代码示例。如果您正苦于以下问题:Java Bytes类的具体用法?Java Bytes怎么用?Java Bytes使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Bytes类属于at.favre.lib.bytes包,在下文中一共展示了Bytes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: obfuscate
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Override
public void obfuscate(@NonNull byte[] original) {
Objects.requireNonNull(original);
Objects.requireNonNull(this.key);
byte[] okm = HKDF.fromHmacSha512().extract(new byte[64], key);
int ctr = 0;
int byteIter = 0;
ByteBuffer buffer = ByteBuffer.wrap(original);
byte[] temp;
byte[] roundKey;
while (buffer.hasRemaining()) {
temp = new byte[Math.min(BLOCK_SIZE_BYTE, buffer.remaining())];
roundKey = HKDF.fromHmacSha512().expand(okm, Bytes.from(ctr++).array(), temp.length);
buffer.get(temp);
for (int i = 0; i < temp.length; i++) {
original[byteIter++] = (byte) (temp[i] ^ roundKey[i]);
}
Bytes.wrap(temp).mutable().fill((byte) 0);
Bytes.wrap(roundKey).mutable().fill((byte) 0);
}
Bytes.wrap(okm).mutable().fill((byte) 0);
}
示例2: getPreferencesRandom
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
private byte[] getPreferencesRandom(StringMessageDigest stringMessageDigest, DataObfuscator dataObfuscator, SecureRandom secureRandom) {
preferenceRandomContentKey = stringMessageDigest.derive(KEY_RANDOM, "prefName");
String base64Random = sharedPreferences.getString(preferenceRandomContentKey, null);
byte[] outBytes;
if (base64Random == null) {
Timber.v("create new preference random");
byte[] rndBytes = Bytes.random(32, secureRandom).array();
outBytes = Bytes.from(rndBytes).array();
dataObfuscator.obfuscate(rndBytes);
sharedPreferences.edit().putString(preferenceRandomContentKey, Bytes.wrap(rndBytes).encodeBase64()).apply();
Bytes.wrap(rndBytes).mutable().secureWipe();
} else {
byte[] obfuscatedRandom = Bytes.parseBase64(base64Random).array();
dataObfuscator.deobfuscate(obfuscatedRandom);
outBytes = obfuscatedRandom;
}
return outBytes;
}
示例3: getStringSet
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Override
public Set<String> getStringSet(String key, Set<String> defaultValues) {
final String keyHash = encryptionProtocol.deriveContentKey(key);
final Set<String> encryptedSet = sharedPreferences.getStringSet(keyHash, null);
if (encryptedSet == null) {
return defaultValues;
}
final Set<String> decryptedSet = new HashSet<>(encryptedSet.size());
for (String encryptedValue : encryptedSet) {
byte[] bytes = decrypt(keyHash, encryptedValue);
if (bytes == null) {
return decryptedSet;
}
decryptedSet.add(Bytes.from(bytes).encodeUtf8());
}
return decryptedSet;
}
示例4: encrypt
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Override
public byte[] encrypt(@NonNull String contentKey, char[] password, byte[] rawContent) throws EncryptionProtocolException {
long start = System.currentTimeMillis();
byte[] fingerprintBytes = new byte[0];
byte[] key = new byte[0];
try {
byte[] contentSalt = Bytes.random(16, secureRandom).array();
fingerprintBytes = fingerprint.getBytes();
key = keyDerivationFunction(contentKey, fingerprintBytes, contentSalt, preferenceSalt, password);
byte[] encrypted = authenticatedEncryption.encrypt(key, compressor.compress(rawContent), Bytes.from(protocolVersion).array());
DataObfuscator obfuscator = dataObfuscatorFactory.create(Bytes.from(contentKey).append(fingerprintBytes).array());
obfuscator.obfuscate(encrypted);
obfuscator.clearKey();
return encode(contentSalt, encrypted);
} catch (AuthenticatedEncryptionException e) {
throw new EncryptionProtocolException(e);
} finally {
Bytes.wrap(fingerprintBytes).mutable().secureWipe();
Bytes.wrap(key).mutable().secureWipe();
Timber.v("encrypt took %d ms", System.currentTimeMillis() - start);
}
}
示例5: request
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Override
public void request(String[] fillArray) {
for (int i = 0; i < fillArray.length; i++) {
byte[] rnd = drbg.nextBytes(length);
if (crc32) {
rnd = Bytes.wrap(rnd).transform(new ByteUtils.Crc32AppenderTransformer()).array();
}
String randomEncodedString = padding ? encoder.encodePadded(rnd) : encoder.encode(rnd);
if (urlencode) {
try {
randomEncodedString = new URLCodec().encode(randomEncodedString);
} catch (EncoderException e) {
throw new IllegalStateException("could not url encode", e);
}
}
fillArray[i] = randomEncodedString;
}
}
示例6: testStringEncoding
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Test
public void testStringEncoding() {
for (int x = 0; x < 5; x++) {
for (int i = 0; i < 32; i++) {
byte[] testArray = Bytes.random(i).array();
String hex = Bytes.from(testArray).encodeHex(true);
String encoded = new String(testArray, StandardCharsets.ISO_8859_1);
System.out.println(encoded);
String hexStringEncoded = Bytes.from(encoded, StandardCharsets.ISO_8859_1).encodeHex(true);
System.out.println(hex);
System.out.println(hexStringEncoded);
assertEquals(hex, hexStringEncoded);
}
}
}
示例7: getRandom
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Test
public void getRandom() throws Exception {
RandomOrgServiceHandler.Result<Void> response = new HotbitsServiceHandler(true).getRandom();
assertNotNull(response);
if (response.throwable != null) {
response.throwable.printStackTrace();
}
if (response.throwable != null && response.throwable instanceof UnknownHostException) {
System.out.printf(response.errorMsg);
} else {
assertNotNull(response.seed);
assertNull(response.response);
System.out.println(Bytes.from(response.seed).encodeHex());
assertTrue(response.seed.length == ENTROPY_SEED_LENGTH_BYTE);
}
}
示例8: getRandom
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Test
public void getRandom() throws Exception {
ServiceHandler.Result<AnuQuantumResponse> response = new AnuQuantumServiceHandler(true).getRandom();
assertNotNull(response);
if (response.throwable != null) {
response.throwable.printStackTrace();
}
if (response.throwable != null && response.throwable instanceof UnknownHostException) {
System.out.printf(response.errorMsg);
} else {
assertNotNull(response.seed);
assertNotNull(response.response);
System.out.println(Bytes.from(response.seed).encodeHex());
assertTrue(response.seed.length == AnuQuantumServiceHandler.ENTROPY_SEED_LENGTH_BYTE);
assertTrue(response.response.success);
assertNotEquals(0, response.response.hashCode());
assertNotNull(response.response.toString());
assertFalse(response.response.equals(new AnuQuantumResponse("", -1, null, null, false)));
assertTrue(response.response.length == 1);
assertTrue(response.response.size == AnuQuantumServiceHandler.ENTROPY_SEED_LENGTH_BYTE);
}
}
示例9: getRandom
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Test
public void getRandom() throws Exception {
RandomOrgServiceHandler.Result<RandomOrgBlobResponse> response = new RandomOrgServiceHandler(true).getRandom();
assertNotNull(response);
if (response.throwable != null) {
response.throwable.printStackTrace();
}
if (response.throwable != null && response.throwable instanceof UnknownHostException) {
System.out.printf(response.errorMsg);
} else {
assertNotNull(response.seed);
assertNotNull(response.response);
System.out.println(Bytes.from(response.seed).encodeHex());
System.out.println(response.response.toString());
assertFalse(response.response.equals(1));
assertFalse(response.response.hashCode() == 0);
assertTrue(response.seed.length == ENTROPY_SEED_LENGTH_BIT / 8);
}
}
示例10: generateExpandableEntropyTest
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Test
public void generateExpandableEntropyTest() throws Exception {
ExpandableEntropySource entropySource = getExpandableEntropySource();
if (entropySource == null) {
return;
}
assertNotNull(entropySource.getInformation());
Set<byte[]> pastSeeds = new HashSet<>();
for (int i = 8; i < 64; i += 8) {
byte[] seed = entropySource.generateEntropy(i);
assertTrue(seed.length == i);
assertFalse(pastSeeds.contains(seed));
pastSeeds.add(seed);
System.out.println(Bytes.from(seed).encodeHex());
}
}
示例11: testSmallArrayInput
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Test
public void testSmallArrayInput() throws Exception {
byte[] b1 = HKDF.fromHmacSha256().extractAndExpand(new byte[16], new byte[]{1}, "smth".getBytes(), 64);
byte[] b2 = HKDF.fromHmacSha256().extractAndExpand(new byte[16], new byte[]{1}, "smth".getBytes(), 64);
byte[] b3 = HKDF.fromHmacSha256().extractAndExpand(new byte[16], new byte[1], "smth".getBytes(), 64);
byte[] b4 = HKDF.fromHmacSha256().extractAndExpand(new byte[16], new byte[2], "smth".getBytes(), 64);
assertArrayEquals(b1, b2);
assertFalse(Arrays.equals(b1, b3));
assertFalse(Arrays.equals(b1, b4));
assertFalse(Arrays.equals(b3, b4));
System.out.println(Bytes.wrap(b1).encodeHex());
System.out.println(Bytes.wrap(b2).encodeHex());
System.out.println(Bytes.wrap(b3).encodeHex());
System.out.println(Bytes.wrap(b4).encodeHex());
}
示例12: derive
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Override
public String derive(String providedMessage, String usageName) {
Objects.requireNonNull(providedMessage);
Objects.requireNonNull(usageName);
return Bytes.wrap(HKDF.fromHmacSha512().extractAndExpand(salt, Bytes.from(providedMessage, Normalizer.Form.NFKD).array(),
Bytes.from(usageName, Normalizer.Form.NFKD).array(), outLength)).encodeHex();
}
示例13: getApkSignatureHash
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
/**
* Gets the SHA-256 hashed fingerprint of the APK signature
*
* @param context from Android
* @return 32 bytes sha256 hash
*/
private static byte[] getApkSignatureHash(Context context) {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
@SuppressLint("PackageManagerGetSignatures")
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
bos.write(signature.toByteArray());
}
return Bytes.wrap(bos.toByteArray()).hashSha256().array();
} catch (Exception e) {
throw new IllegalStateException("could not apk signature has", e);
}
}
示例14: generateSalt
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@NonNull
private static String generateSalt(byte[] salt, int logRounds) {
StringBuilder saltBuilder = new StringBuilder();
saltBuilder.append("$2a$");
if (logRounds < 10) {
saltBuilder.append("0");
}
if (logRounds > 30) {
throw new IllegalArgumentException("log_rounds exceeds maximum (30)");
}
saltBuilder.append(Integer.toString(logRounds));
saltBuilder.append("$");
saltBuilder.append(Bytes.wrap(HKDF.fromHmacSha256().expand(salt, "bcrypt".getBytes(), 16)).encodeHex());
return saltBuilder.toString();
}
示例15: decrypt
import at.favre.lib.bytes.Bytes; //导入依赖的package包/类
@Override
public byte[] decrypt(byte[] rawEncryptionKey, byte[] encryptedData, @Nullable byte[] associatedData) throws AuthenticatedEncryptionException {
try {
ByteBuffer byteBuffer = ByteBuffer.wrap(encryptedData);
int ivLength = byteBuffer.get();
byte[] iv = new byte[ivLength];
byteBuffer.get(iv);
byte[] encrypted = new byte[byteBuffer.remaining()];
byteBuffer.get(encrypted);
final Cipher cipher = getCipher();
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(rawEncryptionKey, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));
if (associatedData != null) {
cipher.updateAAD(associatedData);
}
byte[] decrypted = cipher.doFinal(encrypted);
Bytes.wrap(iv).mutable().secureWipe();
Bytes.wrap(rawEncryptionKey).mutable().secureWipe();
Bytes.wrap(encrypted).mutable().secureWipe();
return decrypted;
} catch (Exception e) {
throw new AuthenticatedEncryptionException("could not decrypt", e);
}
}