All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.artipie.nuget.Hash Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
/*
 * The MIT License (MIT) Copyright (c) 2020-2023 artipie.com
 * https://github.com/artipie/artipie/blob/master/LICENSE.txt
 */

package com.artipie.nuget;

import com.artipie.asto.Content;
import com.artipie.asto.Storage;
import com.artipie.asto.ext.ContentDigest;
import com.artipie.asto.ext.Digests;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.concurrent.CompletionStage;
import org.reactivestreams.Publisher;

/**
 * Package hash.
 *
 * @since 0.1
 */
public final class Hash {

    /**
     * Bytes to calculate hash code value from.
     */
    private final Publisher value;

    /**
     * Ctor.
     *
     * @param value Bytes to calculate hash code value from.
     */
    public Hash(final Publisher value) {
        this.value = value;
    }

    /**
     * Saves hash to storage as base64 string.
     *
     * @param storage Storage to use for saving.
     * @param identity Package identity.
     * @return Completion of save operation.
     */
    public CompletionStage save(final Storage storage, final PackageIdentity identity) {
        return
            new ContentDigest(this.value, Digests.SHA512).bytes().thenCompose(
                bytes -> storage.save(
                    identity.hashKey(),
                    new Content.From(Base64.getEncoder().encode(bytes))
                )
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy