Garbler.java 868 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.oblivm.backend.gc.offline;
  2. import java.nio.ByteBuffer;
  3. import java.security.MessageDigest;
  4. import com.oblivm.backend.gc.GCSignal;
  5. final class Garbler {
  6. private MessageDigest sha1 = null;
  7. Garbler() {
  8. try {
  9. sha1 = MessageDigest.getInstance("SHA-1");
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. System.exit(1);
  13. }
  14. }
  15. public GCSignal enc(GCSignal lb0, GCSignal lb1, long k, GCSignal m) {
  16. return getPadding(lb0, lb1, k).xor(m);
  17. }
  18. public GCSignal dec(GCSignal lb0, GCSignal lb1, long k, GCSignal c) {
  19. return getPadding(lb0, lb1, k).xor(c);
  20. }
  21. ByteBuffer buffer = ByteBuffer.allocate(GCSignal.len * 2 + 8);
  22. private GCSignal getPadding(GCSignal lb0, GCSignal lb1, long k) {
  23. buffer.clear();
  24. sha1.update((buffer.put(lb0.bytes).put(lb1.bytes).putLong(k)));
  25. GCSignal ret = GCSignal.newInstance(sha1.digest());
  26. return ret;
  27. }
  28. }