【问题标题】:How to implement sha-1 with Microsoft Azure key vault with java?如何使用 java 使用 Microsoft Azure 密钥库实现 sha-1?
【发布时间】:2018-07-26 16:52:54
【问题描述】:

我需要在 Java 中使用 Sha1 算法实现带有 Key Vault 的标志,但不支持,我该如何实现,我尝试使用充气城堡进行 de digest 并使用带有 RSNULL 算法的密钥库客户端,但是结果不正确。

    String stringToSign = "holamund";
    byte[] digestSha1Bytes = DigestUtils.sha1(stringToSign.getBytes());
    keyOperationResult = keyVaultClient.sign("AZURE_URL_KEY",   JsonWebKeySignatureAlgorithm.RSNULL, digestSha1Bytes);

输出生成:IoGXIIIZ5ZyM2m7ozz / ET8UOWWTwmKeseJVvs9w9cPHz11wKFZ / ikGx2Sj4Adhcn32QCDBOSv / knaTvPyw + EXkVX / 3NrAmxuIUUGhQF4 / muu7Y2644IWuECXqp8o1iXL8mN7sCEB5sh0sNArK77dvfRk7A0unZR / 82wpFxMjxYYeh8k / CiFRHK / MWX6sZe + 1Rm6vDmkaodyRqR1LsusS0wzOiuVdTXNkyL55MaTs5cLpWIpbHU + H4YaAO1 + B + nFVkJeeDDGbjHvmMO1EO / KT7HSHReOukYR2mwKxklzCZA3DWRp3pSi9Rdirpoc / IvFIOcWcYK44xfx0UKVHDzhZ4w == P>

输出预期:pHOyaoTuOTELmTbfgRPg12tJP0JdjQY1GsdMR63s8L8hMb4lsirmalxSVRm5D2ed2d6PMdMxvA + OjUW / Pxzx5R8M5b3SeIiXde5JloOKoOc2PbKIGJI5Sf7 + yCSowCSgTdxmwkTQdBCZWeRhw1hs5hNJW / uBkbImdF0RtR478JxePH9AYEHOFjanLlI5 / OHzduPS8Px9qzQIr / KYRWk32Z14dUGPctYeT5ttY7lYu4ksTeyCwea5booNaZAN8EnT41s564cCPR2ZdYirzcNnWlTQxD7innpuFWP + rvLZHLYp3y + iiYIU6eyJurDoTUHHzTp + mEQSD / IMtgE43FWb6w == P>

我尝试过使用不同的 api 做摘要,如充气城堡、java 本机等,但没有找到正确的约定来使用 key vault 标志。

【问题讨论】:

  • 你检查relatedquestions了吗?
  • 是的,我没有找到适用于 java 的解决方案,有些人谈论 C# 中的 Digest 实现,但我正在使用 java,并且使用相同的 API,我正在评估迁移到 azure ,但如果我找不到解决方案,那就不可能了。

标签: bouncycastle sha1 azure-keyvault java-security


【解决方案1】:

在 Azure Key Vault 中,RSNULL 算法提供原始的 RSA 签名。为了使用原始RSA 签名算法生成标准SHA1 签名,您必须首先将摘要包装到将被签名的ASN.1 DigestInfo 结构中。以下示例表明:

import java.io.IOException;
import java.security.KeyPair;
import java.security.MessageDigest;
import java.security.Signature;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.models.KeyBundle;
import com.microsoft.azure.keyvault.models.KeyOperationResult;
import com.microsoft.azure.keyvault.webkey.JsonWebKey;
import com.microsoft.azure.keyvault.webkey.JsonWebKeySignatureAlgorithm;

...

/**
 * @param keyVaultClient An instance of Key Vault client.
 * @param keyIdentifer The Key Identifier. This is the kid field of JsonWebKey structure.
 * @param message The message to be signed.
 */
void sampleSHA1SignatureWithAzureKeyVault(KeyVaultClient keyVaultClient, String keyIdentifer, byte[] message) throws Throwable {

    /////////////////////////////////////////
    // Signs message using Azure Key Vault //
    /////////////////////////////////////////

    // Compute SHA1 hash:
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(message);
    byte[] digest = md.digest();

    // Convert SHA1 digest into ASN.1:
    byte[] digestInfo = getDigestInfoForSHA1Digest(digest);

    // Call Azure Key Vault to perform the signature using the server key.
    // Note that we are passing the DigestInfo to RSNULL, instead of SHA1 digest.
    KeyOperationResult result = keyVaultClient.sign(keyIdentifer, JsonWebKeySignatureAlgorithm.RSNULL, digestInfo);
    byte[] signature = result.result();

    /////////////////////////////////////////////////////////
    // Verifies the signature locally, using java.security //
    /////////////////////////////////////////////////////////

    // Read the public key from Azure Key Vault
    KeyBundle keyBundle = keyVaultClient.getKey(keyIdentifer);

    // Initialize java.security instances with the Public Key and message.
    KeyPair kp = keyBundle.key().toRSA(false);
    Signature signImpl = Signature.getInstance("SHA1withRSA");
    signImpl.initVerify(kp.getPublic());
    signImpl.update(message);

    // Verify the signature.
    if (signImpl.verify(signature))
        System.out.println("Signature was verified.");
    else
        System.out.println("Signature verification failed.");
}

byte[] getDigestInfoForSHA1Digest(byte[] digest) {

    // Constructs an ASN.1 DigestInfo structure for the caller-specified SHA1 hash.

    // ASN.1 data:
    byte[] digestInfo = new byte[] { //
            0x30, 0x21, // SEQUENCE DigestInfo (33 bytes) (13 of header + 20 of SHA1 digest)
            0x30, 0x09, // SEQUENCE AlgorithmIdentifier (9 bytes)
            0x06, 0x05, // OBJECT IDENTIFIER algorithm (5 bytes)
            0x2b, 0x0e, 0x03, 0x02, 0x1a, // OID of SHA1 (1.3.14.3.2.26)
            0x05, 0x00, // NUL algorithm parameters (05 00 is the DER encoding for NUL)
            0x04, 0x14, // OCTET STRING digest (20 bytes)
            00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 // SHA1, copied below
    };

    System.arraycopy(digest, 0, digestInfo, 15, digest.length);

    return digestInfo;
}

【讨论】:

    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 2013-11-20
    • 2011-05-30
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多