G
G
Grand Silence2017-07-09 18:08:22
PHP
Grand Silence, 2017-07-09 18:08:22

How to rewrite code from Java to PHP SHA1 hashing?

Hello friends, please help me bring a piece of Java code to PHP. My results differ in terms of the final signature, I do not know what to do, what is the problem.
Java code:

private static String bytesToStringSign(byte[] bArr) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bArr) {
            stringBuilder.append(Integer.toString((b & 255) + 256, 16).substring(1));
        }
        return stringBuilder.toString();
    }

    public static String getSign(File file, String str) throws IOException, NoSuchAlgorithmException {
        InputStream fileInputStream;
        Throwable th;
        MessageDigest instance = MessageDigest.getInstance("SHA-1");
        try {
            fileInputStream = new FileInputStream(file);
            try {
                byte[] bArr = new byte[8192];
                int read = fileInputStream.read(bArr);
                while (read != -1) {
                    instance.update(bArr, 0, read);
                    read = fileInputStream.read(bArr);
                }
                if (read == -1) {
                    byte[] bytes = str.getBytes();
                    instance.update(bytes, 0, bytes.length);
                }
                String a = bytesToStringSign(instance.digest());
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                return a;
            } catch (Throwable th2) {
                th = th2;
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            fileInputStream = null;
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            throw th;
        }
    }

    private static String main() {
    	File image;
    	String SECRET_STRING = "qwertyuiopasdfghjklzxcvbnmqwertyuiopas";
    	String result = getSign(image, SECRET_STRING);
    }

Implementation in PHP:
function GetImageSign($input_image) {
        // получаем 8192 первых байт данных изображения
  $data = substr($input_image['data'], 0, 8192);
  // добавляем секретную строку
  $data .= '"qwertyuiopasdfghjklzxcvbnmqwertyuiopas"';

  // полученный Java Hash - db6f43a2e43eec3e3b90ed9dc17df2a409675d64
  $ar = sha1($data, true);
  $size = strlen($ar);

  $str = '';
  for ($i = 0; $i < $size; $i++) {
    $str .= substr(dechex(((int)$ar[$i] & 255) + 256), 2);
  }
  print_r($str);
  die();
}

I would be grateful for any discrepancies found. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pushkarev, 2017-07-09
@AXP-dev

Why don't you like the default feature? php.net/manual/ru/function.sha1.php
If you need sha1 for files then here is - php.net/manual/ru/function.sha1-file.php

G
Grand Silence, 2017-07-09
@mixtape774

I'm not a javaist, so I misunderstood the code initially. The mistake was stupid. The question can be closed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question