Beispiel #1
0
 public static MD5InputStreamResult generateMD5Result(InputStream toEncode) {
   MD5Digest eTag = new MD5Digest();
   byte[] resBuf = new byte[eTag.getDigestSize()];
   byte[] buffer = new byte[1024];
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   long length = 0;
   int numRead = -1;
   try {
     do {
       numRead = toEncode.read(buffer);
       if (numRead > 0) {
         length += numRead;
         eTag.update(buffer, 0, numRead);
         out.write(buffer, 0, numRead);
       }
     } while (numRead != -1);
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     IOUtils.closeQuietly(out);
     IOUtils.closeQuietly(toEncode);
   }
   eTag.doFinal(resBuf, 0);
   return new MD5InputStreamResult(out.toByteArray(), resBuf, length);
 }
Beispiel #2
0
 public static byte[] md5(byte[] plainBytes) {
   MD5Digest eTag = new MD5Digest();
   byte[] resBuf = new byte[eTag.getDigestSize()];
   eTag.update(plainBytes, 0, plainBytes.length);
   eTag.doFinal(resBuf, 0);
   return resBuf;
 }
  public synchronized String generateSignature(byte[] message) throws Exception {
    // Log.trace("Signed message length is " + message.length);

    try {
      digest.reset();
      cipher.init(true, privateRsaKey);
      digest.update(message, 0, message.length);
      byte[] hash = new byte[digest.getDigestSize()];
      digest.doFinal(hash, 0);
      DigestInfo info = new DigestInfo(new AlgorithmIdentifier(md5, null), hash);
      byte[] bytes = info.getEncoded(ASN1Encoding.DER);
      byte[] signature = cipher.processBlock(bytes, 0, bytes.length);
      return new String(org.bouncycastle.util.encoders.Base64.encode(signature));
    } catch (Exception e) {
      throw new Exception("Ошибка создания подписи:\n" + e.getMessage());
    }
  }
Beispiel #4
0
 /**
  * TODO: Kommentar
  *
  * @param file
  * @return
  */
 public static byte[] checksum(File file) {
   MD5Digest md5 = new MD5Digest();
   try {
     FileInputStream in = new FileInputStream(file);
     byte[] arr = new byte[65535];
     int num;
     do {
       num = in.read(arr);
       if (num == -1) {
         break;
       }
       md5.update(arr, 0, num);
     } while (num == arr.length);
     byte[] ret = new byte[16];
     md5.doFinal(ret, 0);
     return ret;
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return null;
   }
 }
 public synchronized void verifySignature(byte[] message, byte[] signature) throws Exception {
   try {
     digest.reset();
     cipher.init(false, publicRsaKey);
     digest.update(message, 0, message.length);
     byte[] hash = new byte[digest.getDigestSize()];
     digest.doFinal(hash, 0);
     DigestInfo info = new DigestInfo(new AlgorithmIdentifier(md5, null), hash);
     byte[] bytes = info.getEncoded(ASN1Encoding.DER);
     byte[] signatureData = org.bouncycastle.util.encoders.Base64.decode(signature);
     byte[] result = cipher.processBlock(signatureData, 0, signatureData.length);
     if ((result == null) || (result.length < hash.length)) {
       throw new Exception("Invalid signature (1)!");
     }
     if (!compareFromTheEnd(hash, result)) {
       throw new Exception("Invalid signature (2)!");
     }
   } catch (Exception e) {
     throw new Exception("Error checking signature:\n" + e.getMessage());
   }
 }
Beispiel #6
0
 public static byte[] md5(File toEncode) {
   MD5Digest eTag = new MD5Digest();
   byte[] resBuf = new byte[eTag.getDigestSize()];
   byte[] buffer = new byte[1024];
   int numRead = -1;
   InputStream i = null;
   try {
     i = new FileInputStream(toEncode);
     do {
       numRead = i.read(buffer);
       if (numRead > 0) {
         eTag.update(buffer, 0, numRead);
       }
     } while (numRead != -1);
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     IOUtils.closeQuietly(i);
   }
   eTag.doFinal(resBuf, 0);
   return resBuf;
 }