/** * Generate an "anonymized" node ID for use with external services. Currently it just hashes the actual node ID * using SHA-256. * * @return The anonymized ID derived from hashing the node ID. */ public String anonymize() { return Hashing.sha256().hashString(id, StandardCharsets.UTF_8).toString(); } }
/** * If url for schema already exists, return the url. If not create a new temporary schema file and return a the url. */ private Path getOrGenerateSchemaFile(Schema schema) throws IOException { Preconditions.checkNotNull(schema, "Avro Schema should not be null"); String hashedSchema = Hashing.sha256().hashString(schema.toString(), StandardCharsets.UTF_8).toString(); if (!this.schemaPaths.containsKey(hashedSchema)) { Path schemaFilePath = new Path(this.schemaDir, String.valueOf(System.currentTimeMillis() + ".avsc")); AvroUtils.writeSchemaToFile(schema, schemaFilePath, fs, true); this.schemaPaths.put(hashedSchema, schemaFilePath); } return this.schemaPaths.get(hashedSchema); } }
public void testSha256() { HashTestUtils.checkAvalanche(Hashing.sha256(), 100, 0.4); HashTestUtils.checkNo2BitCharacteristics(Hashing.sha256()); HashTestUtils.checkNoFunnels(Hashing.sha256()); HashTestUtils.assertInvariants(Hashing.sha256()); assertEquals("Hashing.sha256()", Hashing.sha256().toString()); }
public static String shaSum256(File file) { try { return Files.asByteSource(file).hash(Hashing.sha256()).toString(); } catch (IOException e) { throw new RuntimeException(String.format("Failed to calculate shaSum256 of %s", file)); } }
/** * Creates a snapshot ID from the given string. * * @param id the string from which to create the identifier * @return the snapshot identifier */ public static PrimitiveId from(String id) { return from(Hashing.sha256().hashString(id, StandardCharsets.UTF_8).asLong()); }
private void onSyncPackageJson(Config conf, Path workDirectory, Throwing.Consumer<String> action) throws IOException { Path tmp = Paths.get(conf.getString("application.tmpdir"), "package.json"); Files.createDirectories(tmp); String sha1 = Hashing.sha256() .hashBytes(Files.readAllBytes(workDirectory.resolve("package.json"))) .toString(); Path lastSha1 = tmp.resolve(sha1); if (!Files.exists(lastSha1) || !Files.exists(workDirectory.resolve("node_modules"))) { action.accept("install"); Try.of(Files.walk(tmp)) .run(files -> files.filter(f -> !f.equals(tmp)).forEach(throwingConsumer(Files::deleteIfExists))); Files.write(tmp.resolve(lastSha1), Arrays.asList("")); } }
/** * Returns the partition for the given key. * * @param key the key for which to return the partition * @return the partition for the given key */ default Partition getPartition(String key) { int hashCode = Hashing.sha256().hashString(key, StandardCharsets.UTF_8).asInt(); return getPartition(getPartitionIds().get(Math.abs(hashCode) % getPartitionIds().size())); }
private static void setEncryptionHeaders( HttpHeaders headers, String headerPrefix, Map<Option, ?> options) { String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options); if (key != null) { BaseEncoding base64 = BaseEncoding.base64(); HashFunction hashFunction = Hashing.sha256(); headers.set(headerPrefix + "algorithm", "AES256"); headers.set(headerPrefix + "key", key); headers.set( headerPrefix + "key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes())); } }
@Override public void run() { byte[] hash = Hashing.sha256().hashBytes(data).asBytes(); String hashStr = BaseEncoding.base16().encode(hash); archive.setHash(hash); String path = new StringBuilder() .append(hashStr.substring(0, 2)) .append('/') .append(hashStr.substring(2)) .toString(); try { try (InputStream in = minioClient.getObject(minioBucket, path)) { return; // already exists } catch (ErrorResponseException ex) { // doesn't exist } minioClient.putObject(minioBucket, path, new ByteArrayInputStream(data), data.length, "binary/octet-stream"); } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidArgumentException | InvalidBucketNameException | NoResponseException | IOException | InvalidKeyException | NoSuchAlgorithmException | XmlPullParserException ex) { logger.warn("unable to upload data to store", ex); } }
@Description("compute sha256 hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice sha256(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Hashing.sha256().hashBytes(slice.getBytes()).asBytes()); }
public static Model deserialize(Slice slice) { int version = slice.getInt(VERSION_OFFSET); checkArgument(version == CURRENT_FORMAT_VERSION, format("Unsupported version: %d", version)); byte[] modelHashBytes = slice.getBytes(HASH_OFFSET, 32); HashCode expectedHash = HashCode.fromBytes(modelHashBytes); HashCode actualHash = Hashing.sha256().hashBytes(slice.getBytes(ALGORITHM_OFFSET, slice.length() - ALGORITHM_OFFSET)); checkArgument(actualHash.equals(expectedHash), "model hash does not match data"); int id = slice.getInt(ALGORITHM_OFFSET); Class<? extends Model> algorithm = MODEL_SERIALIZATION_IDS.inverse().get(id); requireNonNull(algorithm, format("Unsupported algorith %d", id)); int hyperparameterLength = slice.getInt(HYPERPARAMETER_LENGTH_OFFSET); byte[] hyperparameterBytes = slice.getBytes(HYPERPARAMETERS_OFFSET, hyperparameterLength); int dataLengthOffset = HYPERPARAMETERS_OFFSET + hyperparameterLength; long dataLength = slice.getLong(dataLengthOffset); int dataOffset = dataLengthOffset + SIZE_OF_LONG; byte[] data = slice.getBytes(dataOffset, (int) dataLength); try { Method deserialize = algorithm.getMethod("deserialize", byte[].class); return (Model) deserialize.invoke(null, new Object[] {data}); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } }
public void testToString() { assertEquals("Hashing.md5()", Hashing.md5().toString()); assertEquals("Hashing.sha1()", Hashing.sha1().toString()); assertEquals("Hashing.sha256()", Hashing.sha256().toString()); assertEquals("Hashing.sha512()", Hashing.sha512().toString()); }
final HashCode hashCode = Hashing.sha256().hashBytes(fileContents); final EntityTag entityTag = new EntityTag(hashCode.toString());
hf = Hashing.sha256(); break;
slice.setBytes(dataOffset, data); byte[] modelHash = Hashing.sha256().hashBytes(slice.getBytes(ALGORITHM_OFFSET, slice.length() - ALGORITHM_OFFSET)).asBytes(); checkState(modelHash.length == 32, "sha256 hash code expected to be 32 bytes"); slice.setBytes(HASH_OFFSET, modelHash);
HashCode rsDataHash = Hashing.sha256().hashBytes(rsData);
if (key != null) { BaseEncoding base64 = BaseEncoding.base64(); HashFunction hashFunction = Hashing.sha256(); requestHeaders.set("x-goog-encryption-algorithm", "AES256"); requestHeaders.set("x-goog-encryption-key", key);
private String tryFindEnumType(List<String> enums) { if (enums != null && !enums.isEmpty()) { String strEnums = enums.toString(); String enumName = "Enum_" + Hashing.sha256().hashString(strEnums, StandardCharsets.UTF_8).toString(); pending.add(() -> createEnum(enumName, enums)); return enumName; } return null; }
@SuppressWarnings("rawtypes") public static Class<? extends Enum> getOrCreateEnumWithPackageName(ClassLoader classLoader, String packageName, List<String> enums) { String strEnums = enums.toString(); String enumClsName = packageName + ".Enum_" + Hashing.sha256().hashString(strEnums, StandardCharsets.UTF_8).toString(); return JavassistUtils.getOrCreateEnumWithClassName(classLoader, enumClsName, enums); }
private String createUniqueDeployId() { UUID id = UUID.randomUUID(); String result = Hashing.sha256().newHasher().putLong(id.getLeastSignificantBits()).putLong(id.getMostSignificantBits()).hash().toString(); return result.substring(0, deployIdLength); }