Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f098c4bb04e17cde5604032029571da397e7e16d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
* Copyright (c) 2007 compeople AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* 	compeople AG (Stefan Liebig) - initial API and implementation
*  IBM - continuing development
*******************************************************************************/
package org.eclipse.equinox.internal.p2.artifact.processors.verifier;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.artifact.processors.Activator;
import org.eclipse.equinox.p2.artifact.repository.IArtifactDescriptor;
import org.eclipse.equinox.p2.artifact.repository.processing.ProcessingStep;
import org.eclipse.equinox.p2.artifact.repository.processing.ProcessingStepDescriptor;

public class MD5Verifier extends ProcessingStep {

	protected String md5Test;
	private MessageDigest md5;

	public MD5Verifier() {
		super();
	}

	public MD5Verifier(String md5Test) {
		super();
		this.md5Test = md5Test;
		basicInitialize(null);
	}

	public void initialize(ProcessingStepDescriptor descriptor, IArtifactDescriptor context) {
		super.initialize(descriptor, context);
		String data = descriptor.getData();
		if (data.equals("download"))
			md5Test = context.getProperty(IArtifactDescriptor.DOWNLOAD_MD5);
		else if (data.equals("artifact"))
			md5Test = context.getProperty(IArtifactDescriptor.ARTIFACT_MD5);
		else
			md5Test = data;
		basicInitialize(descriptor);
	}

	private void basicInitialize(ProcessingStepDescriptor descriptor) {
		int code;
		if (descriptor == null)
			code = IStatus.ERROR;
		else
			code = descriptor.isRequired() ? IStatus.ERROR : IStatus.INFO;
		if (md5Test == null || md5Test.length() != 32)
			status = new Status(code, Activator.ID, "MD5 value not available or incorrect size");
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			status = new Status(code, Activator.ID, "Could not create MD5 algorithm", e);
		}
	}

	public void write(int b) throws IOException {
		if (b != -1)
			md5.update((byte) b);
		destination.write(b);
	}

	public void close() throws IOException {
		super.close();

		byte[] digest = md5.digest();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < digest.length; i++) {
			if ((digest[i] & 0xFF) < 0x10)
				buf.append('0');
			buf.append(Integer.toHexString(digest[i] & 0xFF));
		}

		// if the hashes don't line up set the status to error.
		if (!buf.toString().equals(md5Test)) {
			String message = "Error processing stream. MD5 hash is not as expected.";
			status = new Status(IStatus.ERROR, "plugin id", message);
		} else
			status = Status.OK_STATUS;
	}
}

Back to the top