aboutsummaryrefslogtreecommitdiffstats
path: root/hosted-api/src/main/java/ai/vespa/hosted/api/Signatures.java
blob: 9d2938e45d64d260d3fc688f8a6939891fd274af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.api;

import java.io.InputStream;
import java.net.URI;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Callable;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @author jonmv
 */
public class Signatures {

    /** Returns the SHA-256 hash of the content in the implied input stream. */
    public static byte[] sha256Digest(Callable<InputStream> in) {
        try (DigestInputStream digester = sha256Digester(in.call())) {
            byte[] buffer = new byte[1 << 10];
            while (digester.read(buffer) != -1); // Consume the stream to compute the digest.

            return digester.getMessageDigest().digest();
        }
        catch (IllegalStateException e) {
            throw e;
        }
        catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    /** Wraps the given input stream in a digester which computes a SHA 256 hash of the contents read through it. */
    public static DigestInputStream sha256Digester(InputStream in) {
        try {
            return new DigestInputStream(in, MessageDigest.getInstance("SHA-256"));
        }
        catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
    }

    /** Returns a canonical representation of the given request data. */
    public static byte[] canonicalMessageOf(String method, URI requestUri, String timestamp, String hash) {
        return (method + "\n" + requestUri.normalize() + "\n" + timestamp + "\n" + hash).getBytes(UTF_8);
    }

}