aboutsummaryrefslogtreecommitdiff
path: root/samples/src/jvmMain/java/okio/samples/Hashing.java
blob: 0f2b447442a1c50dd7548b722338b3f1e0316d01 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2018 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okio.samples;

import java.io.File;
import java.io.IOException;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.ByteString;
import okio.HashingSink;
import okio.HashingSource;
import okio.Okio;
import okio.Source;

public final class Hashing {
  public void run() throws Exception {
    File file = new File("../README.md");

    System.out.println("ByteString");
    ByteString byteString = readByteString(file);
    System.out.println("       md5: " + byteString.md5().hex());
    System.out.println("      sha1: " + byteString.sha1().hex());
    System.out.println("    sha256: " + byteString.sha256().hex());
    System.out.println("    sha512: " + byteString.sha512().hex());
    System.out.println();

    System.out.println("Buffer");
    Buffer buffer = readBuffer(file);
    System.out.println("       md5: " + buffer.md5().hex());
    System.out.println("      sha1: " + buffer.sha1().hex());
    System.out.println("    sha256: " + buffer.sha256().hex());
    System.out.println("    sha512: " + buffer.sha512().hex());
    System.out.println();

    System.out.println("HashingSource");
    try (HashingSource hashingSource = HashingSource.sha256(Okio.source(file));
         BufferedSource source = Okio.buffer(hashingSource)) {
      source.readAll(Okio.blackhole());
      System.out.println("    sha256: " + hashingSource.hash().hex());
    }
    System.out.println();

    System.out.println("HashingSink");
    try (HashingSink hashingSink = HashingSink.sha256(Okio.blackhole());
         BufferedSink sink = Okio.buffer(hashingSink);
         Source source = Okio.source(file)) {
      sink.writeAll(source);
      sink.close(); // Emit anything buffered.
      System.out.println("    sha256: " + hashingSink.hash().hex());
    }
    System.out.println();

    System.out.println("HMAC");
    ByteString secret = ByteString.decodeHex("7065616e7574627574746572");
    System.out.println("hmacSha256: " + byteString.hmacSha256(secret).hex());
    System.out.println();
  }

  public ByteString readByteString(File file) throws IOException {
    try (BufferedSource source = Okio.buffer(Okio.source(file))) {
      return source.readByteString();
    }
  }

  public Buffer readBuffer(File file) throws IOException {
    try (Source source = Okio.source(file)) {
      Buffer buffer = new Buffer();
      buffer.writeAll(source);
      return buffer;
    }
  }

  public static void main(String[] args) throws Exception {
    new Hashing().run();
  }
}