bumptech / bumptech/glide

Glide downloaded image hash doesn't match

Open
#4,625 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

**Glide Version**: 4.11.0

**Integration libraries**: no libraries integrated

**Device/Android Version**: Oppo Reno

**Issue details / Repro steps / Use case background**: I need to download images from remote and check if the downloaded ones are correct. If they are, I need to cache them to reduce network connections in the next openings of the app.
Actually I managed to download an image as Bitmap, convert it into byte array and calculate its SHA256 hash but when I compare it to the hash of the same image calculated with an online service, for example this: https://emn178.github.io/online-tools/sha256_checksum.html the two hashes don't match.
The image that I'm testing with is this: http://www.starcoppe.it/images/grafica-immagine-s.jpg
The hash calculated with the online tool is 33dc0df4ee0397f6977a9b833c180f2a60504cb9da89c9b173807fcc2781284d
The hash calculated with my code is 24d23d798b53d02be5db008b038d191ddd4fdc7395bb10528bc373d6d0175627

The code listed below is from a sample project and, other than this code in the MainActivity, it is the default project.

**Glide load line / `GlideModule` (if any) / list Adapter code (if any)**:
```java
Glide.with(context.getApplicationContext())
.asBitmap()
.load(uri)
.listener(new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Log.e(TAG, "download failed!");
return false;
}

@Override
public boolean onResourceReady(Bitmap resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
Log.d(TAG, "download successful!");
int size = resource.getRowBytes() * resource.getHeight();
ByteBuffer b = ByteBuffer.allocate(size);

resource.copyPixelsToBuffer(b);
b.rewind();

byte[] bytes = new byte[size];

try {
b.get(bytes, 0, bytes.length);
Log.d(TAG, "result: " + checkIntegrity(bytes));
} catch (BufferUnderflowException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return false;
}
})
.into(target);

[...]

public boolean checkIntegrity(byte[] bytes) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedhash = digest.digest(bytes);
String calculatedHash = bytesToHex(encodedhash);
return calculatedHash.equalsIgnoreCase(checkHash);
}

public String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
```

**Layout XML**:
```xml

```

**Stack trace / LogCat**: There is no error because the download and load into the target view is successful.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.