我正試圖動態生成一個密碼,以便將每個api請求發送到mpesa apis。根據文檔,我需要首先連接提供的密鑰與時間戳和商人ID,然後做一個sha256來獲得一個散列密碼,然後我應該編碼到基礎64然而,在Java中這樣做返回一個錯誤的編碼字符串,而類似的過程和代碼在PHP中工作,我可能會在哪裏出錯?如何在java中對mpesa密碼進行哈希和編碼
import org.apache.commons.codec.binary.Base64;
import org.apache.cxf.headers.Header;
import org.apache.cxf.jaxb.JAXBDataBinding;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
public class CheckoutHeaderBuilder {
public static Header buildHeader(String merchantId, String passKey, String timestamp) throws JAXBException, NoSuchAlgorithmException, UnsupportedEncodingException {
String hashedEncodedPass = Base64.encodeBase64String(MessageDigest.getInstance("SHA-256").digest((merchantId + passKey + timestamp).getBytes("utf-8")));
List<Header> headers = new ArrayList<>();
CheckOutHeader checkOutHeader = new CheckOutHeader();
checkOutHeader.setTIMESTAMP(timestamp);
checkOutHeader.setMERCHANTID(merchantId);
checkOutHeader.setPASSWORD(hashedEncodedPass);
return new Header(new QName("tns:ns", "CheckOutHeader"), checkOutHeader, new JAXBDataBinding(CheckOutHeader.class));
}
}
@zaph指出,我已經編輯了標題相應 –