NetSuite SuiteScript 2.0字符串SHA1转BASE64编码报错求助
You're running into this error:
WrappedException: Wrapped java.lang.IllegalArgumentException: contains illegal character for hexBinary: crypto.SecretKey
Let's break down why this is happening and fix your code.
The Root Cause
Your code misuses HTTPS.createSecretKey() — this method is meant for creating secret keys from GUIDs or raw data, not converting a hashed SecureString to a hexadecimal string. When you pass the hashed SecureString directly to the guid parameter, you're passing a crypto.SecretKey object instead of a valid hex string, which triggers the illegal character error.
Corrected Code
Here's the working version that correctly executes your required steps (SHA1 hex hash → Base64 encode):
// Assume nonce, timestamp, secret are pre-defined variables var str = nonce + timestamp + secret; var secureString = HTTPS.createSecureString({ input: str }); // Step 1: Compute SHA1 hash of the input string var hashedSecureString = secureString.hash({ algorithm: CRYPTO.HashAlg.SHA1 }); // Convert the hashed SecureString to a hexadecimal string var hexString = hashedSecureString.toString({ encoding: HTTPS.Encoding.HEX }); // Step 2: Encode the hex string to Base64 var digest = ENCODE.convert({ string: hexString, inputEncoding: ENCODE.Encoding.HEX, outputEncoding: ENCODE.Encoding.BASE_64 });
What Changed?
- Hex string conversion: Instead of
HTTPS.createSecretKey(), we usehashedSecureString.toString({ encoding: HTTPS.Encoding.HEX })— this is the correct way to get a hexadecimal representation of a hashedSecureStringin SuiteScript 2.0. - Valid input for Base64 conversion: Now
hexStringis a plain text hex string, whichENCODE.convert()can properly process without throwing errors.
内容的提问来源于stack exchange,提问作者david_85

