Skip to main content
summaryrefslogtreecommitdiffstats
blob: 50d2ff0d3630f6c075cc4633b5c637bc24186487 (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
/*******************************************************************************
 * Copyright (c) 2007, 2018 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 Corporation - ongoing development
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.artifact.processors.md5;

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.repository.Activator;
import org.eclipse.equinox.internal.p2.repository.helpers.ChecksumHelper;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing.ProcessingStep;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.IProcessingStepDescriptor;
import org.eclipse.osgi.util.NLS;

@Deprecated
public class MD5Verifier extends ProcessingStep {

	protected String expectedMD5;
	private MessageDigest md5;

	public MD5Verifier() {
		super();
	}

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

	//This handle the case where the MD5 verification is initiated by a processing step
	@Override
	public void initialize(IProvisioningAgent agent, IProcessingStepDescriptor descriptor, IArtifactDescriptor context) {
		super.initialize(agent, descriptor, context);
		String data = descriptor.getData();
		if (IArtifactDescriptor.DOWNLOAD_MD5.equals(data))
			expectedMD5 = context.getProperty(IArtifactDescriptor.DOWNLOAD_MD5);
		else if (IArtifactDescriptor.ARTIFACT_MD5.equals(data))
			expectedMD5 = context.getProperty(IArtifactDescriptor.ARTIFACT_MD5);
		else
			expectedMD5 = data;
		basicInitialize(descriptor);
	}

	private void basicInitialize(IProcessingStepDescriptor descriptor) {
		int code = (descriptor == null) ? IStatus.ERROR : descriptor.isRequired() ? IStatus.ERROR : IStatus.INFO;
		if (expectedMD5 == null || expectedMD5.length() != 32)
			setStatus(new Status(code, Activator.ID, NLS.bind(Messages.Error_invalid_hash, expectedMD5)));
		try {
			md5 = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
		} catch (NoSuchAlgorithmException e) {
			setStatus(new Status(code, Activator.ID, Messages.Error_MD5_unavailable, e));
		}
	}

	@Override
	public void write(int b) throws IOException {
		md5.update((byte) b);
		getDestination().write(b);
	}

	@Override
	public void close() throws IOException {
		byte[] digest = md5.digest();
		String buf = ChecksumHelper.toHexString(digest);

		// if the hashes don't line up set the status to error.
		if (!buf.equals(expectedMD5))
			setStatus(new Status(IStatus.ERROR, Activator.ID, ProvisionException.ARTIFACT_MD5_NOT_MATCH, NLS.bind(Messages.Error_unexpected_hash, expectedMD5, buf), null));
		super.close();
	}
}

Back to the top