SHA1Native.java

  1. /*
  2.  * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.util.sha1;

  11. import java.security.MessageDigest;

  12. import org.eclipse.jgit.lib.Constants;
  13. import org.eclipse.jgit.lib.MutableObjectId;
  14. import org.eclipse.jgit.lib.ObjectId;

  15. /**
  16.  * SHA1 implementation using native implementation from JDK. It doesn't support
  17.  * collision detection but is faster than the pure Java implementation.
  18.  */
  19. class SHA1Native extends SHA1 {

  20.     private final MessageDigest md;

  21.     SHA1Native() {
  22.         md = Constants.newMessageDigest();
  23.     }

  24.     @Override
  25.     public void update(byte b) {
  26.         md.update(b);
  27.     }

  28.     @Override
  29.     public void update(byte[] in) {
  30.         md.update(in);
  31.     }

  32.     @Override
  33.     public void update(byte[] in, int p, int len) {
  34.         md.update(in, p, len);
  35.     }

  36.     @Override
  37.     public byte[] digest() throws Sha1CollisionException {
  38.         return md.digest();
  39.     }

  40.     @Override
  41.     public ObjectId toObjectId() throws Sha1CollisionException {
  42.         return ObjectId.fromRaw(md.digest());
  43.     }

  44.     @Override
  45.     public void digest(MutableObjectId id) throws Sha1CollisionException {
  46.         id.fromRaw(md.digest());
  47.     }

  48.     @Override
  49.     public SHA1 reset() {
  50.         md.reset();
  51.         return this;
  52.     }

  53.     @Override
  54.     public SHA1 setDetectCollision(boolean detect) {
  55.         return this;
  56.     }

  57.     @Override
  58.     public boolean hasCollision() {
  59.         return false;
  60.     }
  61. }