package peergos.shared.crypto.hash;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {
    public static final String HASH = "SHA-256";

    public static byte[] sha256(byte[] input)
    {
        try {
            MessageDigest md = MessageDigest.getInstance(HASH);
            md.update(input);
            return md.digest();
        } catch (NoSuchAlgorithmException e)
        {
            // This is only here to work around a bug in Doppio JVM
            Sha256 sha256 = new Sha256();
            sha256.update(input);
            byte[] hash = sha256.digest();
            return hash;
        }
    }

    public static byte[] sha256(String password)
    {
        try {
            return sha256(password.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e)
        {
            throw new IllegalStateException("couldn't hash password");
        }
    }


}

@@ -30,16 +30,50 @@
 .hash;%0A%0A
+import peergos.shared.user.fs.*;%0A%0A
 import j
@@ -109,16 +109,16 @@
 eption;%0A
-
 import j
@@ -191,16 +191,47 @@
 ception;
+%0Aimport java.util.concurrent.*;
 %0A%0Apublic
@@ -339,20 +339,16 @@
 %5D input)
-%0A   
  %7B%0A     
@@ -522,32 +522,24 @@
 Exception e)
-%0A       
  %7B%0A         
@@ -776,30 +776,49 @@
  static 
+CompletableFuture%3C
 byte%5B%5D
+%3E
  sha256(
 String p
@@ -809,16 +809,1355 @@
  sha256(
+AsyncReader input, long length) %7B%0A        try %7B%0A            MessageDigest md = MessageDigest.getInstance(HASH);%0A            return sha256(input, length, md, new byte%5BChunk.MAX_SIZE%5D);%0A        %7D catch (NoSuchAlgorithmException e) %7B%0A            // This is only here to work around a bug in Doppio JVM%0A            Sha256 sha256 = new Sha256();%0A            return sha256(input, length, sha256, new byte%5BChunk.MAX_SIZE%5D);%0A        %7D%0A    %7D%0A%0A    private static CompletableFuture%3Cbyte%5B%5D%3E sha256(AsyncReader input, long length, MessageDigest md, byte%5B%5D buf) %7B%0A        if (length == 0)%0A            return CompletableFuture.completedFuture(md.digest());%0A        return input.readIntoArray(buf, 0, buf.length)%0A                .thenCompose(read -%3E %7B%0A                    md.update(buf, 0, read);%0A                    return sha256(input, length - read, md, buf);%0A                %7D);%0A    %7D%0A%0A    private static CompletableFuture%3Cbyte%5B%5D%3E sha256(AsyncReader input, long length, Sha256 md, byte%5B%5D buf) %7B%0A        if (length == 0)%0A            return CompletableFuture.completedFuture(md.digest());%0A        return input.readIntoArray(buf, 0, buf.length)%0A                .thenCompose(read -%3E %7B%0A                    md.update(buf, 0, read);%0A                    return sha256(input, length - read, md, buf);%0A                %7D);%0A    %7D%0A%0A    public static byte%5B%5D sha256(
 String p
@@ -2164,20 +2164,16 @@
 assword)
-%0A   
  %7B%0A     
@@ -2232,24 +2232,24 @@
 (%22UTF-8%22));%0A
+
         %7D ca
@@ -2284,24 +2284,16 @@
 ption e)
-%0A       
  %7B%0A     

Implement streaming sha256 calculation