首页 > 解决方案 > Hmac.digest 返回 AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID

问题描述

我正在尝试创建一个使用套件脚本 2.0 生成 OAuth 签名的函数。但是,我遇到“AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID - 解密凭据时出错”。使用 hmac.digest 函数时出错。有什么想法我哪里出错了吗?

var generateOAuthSignature = function(params) {
    require(['N/crypto', 'N/encode'], function(crypto, encode) {
        var timestamp = Math.round((new Date()).getTime() / 1000.0);
        var nonce = encode.convert({
            string: timestamp + "",
            inputEncoding: encode.Encoding.UTF_8,
            outputEncoding: encode.Encoding.BASE_64
        });

        var oauth_headers = {
            oauth_version: "1.0",
            oauth_nonce: nonce,
            oauth_signature_method: params.signature_method,
            oauth_consumer_key: params.consumer_key,
            oauth_token: params.token,
            oauth_timestamp: timestamp
        };

        log.debug('oauth_headers', oauth_headers)

        var url_params = params.rest_url.split("?")[1].split("&");
        var signature_params = {};

        for (key in oauth_headers) {
            signature_params[key] = key + "=" + oauth_headers[key];
        }

        for (key in url_params) {
            var temp = url_params[key].split("=");
            signature_params[temp[0]] = url_params[key];
        }

        var signature_string = "";
        var sortedkeys = Object.keys(signature_params).sort();

        for (var i = 0; i < sortedkeys.length; i++) {
            signature_string += (i == 0 ? "" : "&") + signature_params[sortedkeys[i]];
        }

        var base_string = params.method + "&" + encodeURIComponent(params.rest_url.split("?")[0]) + "&" + encodeURIComponent(signature_string);
        var composite_key_guid = encodeURIComponent(params.consumer_secret) + "&" + encodeURIComponent(params.token_secret);

        log.debug('composite_key_guid', composite_key_guid);
        log.debug("crypto.createSecretKey({guid: composite_key, encoding: encode.Encoding.UTF_8})", JSON.stringify(crypto.createSecretKey({ guid: composite_key_guid, encoding: encode.Encoding.UTF_8 })));

        var secret_key = crypto.createSecretKey({
            guid: composite_key_guid, encoding: encode.Encoding.UTF_8
        });

        log.debug('secret_key', secret_key);

        var hmac = crypto.createHmac({
            algorithm: crypto.HashAlg.SHA256,
            key: secret_key
        });

        hmac.update({
            input: base_string,
            inputEncoding: encode.Encoding.UTF_8
        });

        // error occuring here 
        oauth_signature = hmac.digest({
            outputEncoding: encode.Encoding.BASE_64
        });

        //more code below...
    });
}

标签: javascriptnetsuitehmaccryptojssuitescript

解决方案


问题是guid. NetSuiteguid不是您编造的东西,它是“对表单凭据字段中的密码值的引用”。

资源:https://{yourAppId}.app.netsuite.com/app/help/helpcenter.nl?fid=section_4358549582.html

制作凭证字段是一种痛苦。即使在 XML 中定义 PASSWORD 字段类型也不会这样做。

/**
 * @NApiVersion 2.x
 */

require(['N/crypto', 'N/encode', 'N/runtime'], function(crypto, encode, runtime) {
    function createSecureKeyWithHash() {
        var inputString = 'YWJjZGVmZwo=';
        // the guid MUST reference a credential field in NetSuite
        var myGuid = '{284CFB2D225B1D76FB94D150207E49DF}';

         
        // if the guid is invalid, this doesn't really return anything
        var sKey = crypto.createSecretKey({
            guid: myGuid,
            encoding: encode.Encoding.UTF_8
        });

        // similarly, since the secret key is invalid, this silently returns nothing
        var hmacSHA512 = crypto.createHmac({
            algorithm: crypto.HashAlg.SHA512,
            key: sKey
        });
        hmacSHA512.update({
            input: inputString,
            inputEncoding: encode.Encoding.BASE_64
        });
        
        // by the time you get here, all the methods above will have run, but they
        // haven't actually returned anything, because the guid was bad. This is 
        // first time the code throws and error
        var digestSHA512 = hmacSHA512.digest({
            outputEncoding: encode.Encoding.HEX
        });
    }

    createSecureKeyWithHash();
});

推荐阅读