Skip to content

informat.codec Encoding/Decoding

Overview

Use informat.codec to perform encoding, decoding, signing, and other functions.

hash

Get the hash value of a string

javascript
informat.codec.hash(str, method);
ParameterTypeDescription
strStringString to calculate hash value for
methodStringHash function, optional methods include md5,sha1,sha256,sha224,sha384,sha512

Return Value

Type String, returns the hash value of str

Example

javascript
informat.codec.hash("hello informat", "md5");
console.log(md5Result);
text
6de76e2f68123fd1a06b86a5dabca022

base64Encode

Get the base64 encoding of a string

javascript
informat.codec.base64Encode(str);
ParameterTypeDescription
strStringString to encode

Return Value

Type String, returns the base64 value of str

Example

javascript
informat.codec.base64Encode("hello informat");
text
aGVsbG8gaW5mb3JtYXQ=

base64EncodeToBytes

Get the base64 encoding of a string as bytes

javascript
informat.codec.base64EncodeToBytes(str);
ParameterTypeDescription
strStringString to encode

Return Value

Type Array<Byte>, returns the base64 value of str

Example

javascript
informat.codec.base64EncodeToBytes("hello informat");
text
"YUdWc2JHOGdhVzVtYjNKdFlYUT0="

base64Decode

Return base64 decoded byte array

javascript
informat.codec.base64Decode(str);
ParameterTypeDescription
strStringBase64 encoded string

Return Value

Type Array<Byte>, returns the base64 decoded value of str

Example

javascript
informat.codec.base64Decode("aGVsbG8gaW5mb3JtYXQ=");
text
'aGVsbG8gaW5mb3JtYXQ='

base64DecodeToString

Return base64 decoded string

javascript
informat.codec.base64DecodeToString(str);
ParameterTypeDescription
strStringBase64 encoded string

Return Value

Type String, returns the base64 decoded string of str

Example

javascript
informat.codec.base64DecodeToString("aGVsbG8gaW5mb3JtYXQ=");
text
'hello informat'

base64DecodeFromBytes

Return base64 decoded byte array from bytes

javascript
informat.codec.base64DecodeFromBytes(bytes);
ParameterTypeDescription
bytesArray<Byte>Base64 encoded byte array

Return Value

Type Array<Byte>, returns the base64 decoded value

Example

javascript
const encode = informat.codec.base64EncodeToBytes("hello informat");
const decode = informat.codec.base64DecodeFromBytes(encode);
text
aGVsbG8gaW5mb3JtYXQ=

sign

Sign a string using a signature algorithm

javascript
informat.codec.sign(str, method, privateKey);
ParameterTypeDescription
strStringString to sign
methodStringSignature method
privateKeyStringRSA private key used for signing

Supported signature methods:

  • MD2withRSA
  • MD5withRSA
  • SHA1withRSA
  • SHA224withRSA
  • SHA256withRSA
  • SHA384withRSA
  • SHA512withRSA
  • MD5andSHA1withRSA

Return Value

Type String, returns the signature result of str

Example

javascript
const privateKey = `MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJyC69MovQ1Y6pNGPFv3CV8gYaRYE1ac6Vh34gscZhgkuAgjkDnu9r5o3DDclqkAoZtIcMIrzfKpIQJ3b7Jjf5XXVikIHwFgJXfl3sklYLqcNKvDVjF0NTgvj1Mf+fC/5nKa4OlHjVuCfcWr1zVFVo1JgjW9gBAH7zHoImTOeHa3AgMBAAECgYANIpm1xOH/ku+g65CY6/B/6MujtiFCsB7kpgRfsq2eJ3AV0seXF8c32hISEblH4WiEC+jWtZluXPkLEsivhn/dtbT4qjQcgADZUl63SN2r/3sLZDAlqxTEtuPEfmqXV2+/faDU3xSf2F7RNXRkBzNDfYgRcJfeGa8uxeRw3TrOnQJBAMrq1YA/Ck32IoXJo3qOlOhy8O2nYS3aJhHKbDiDeJGtdn4JZFjBrMbSb9e9xQa6wTiK74hFGQv34CrM8o/ksWUCQQDFdFStr9JDztEqHuwExuvFYSQxG+1go/vYaV7qy2KDVPwEf/ZRdCT4gKaT2TNiTVKge1Qr4E2xgXbI/6VZgLPrAkEAk2YejLg+Tf97eF/OhtpHxMqpxJiPePU8LjQyhKLL1FaC+m7sG6UkUpDOeZL6KjdC4EXcVcqLtSvsBGs3z9q6GQJBAJrAxe0qs7z5Ru2gNpK35OlZbSggHzdyzluamg2jQZ506OAN+lt0j9VD30pZHPCacXvdrOaGcd4A/bwiwNEZEekCQDmXdqJlQV8mLsa1B7HCecR8L/PRiYH8xhWlqNS7EDk3Cc5T3FGk2NImNBgkpWtEOo3sdJsmIbBMkhViiR/FTqg=`;
const signResult = informat.codec.sign("hello world", "SHA256withRSA", privateKey);
text
SxWsYRAnMrmKWRW5jEIsI0HiWEXc5Ed6iVC6CZH/t5nCFMu5hRDeJkJW9bpL1SDVJcvMDqKQ9lVuIDutcnote2rU7yxAiOr0pgmBDLjrzPY5eXnsYow/yPL5s8ujhmzrkC9mbjR9tumPHzG2PWqTXX6cxuQ5NjzPqqduDEs0w/g=

rsaEncryptHexByPrivateKey

RSA private key encryption (returns hex string) (usually used for signing)

javascript
informat.codec.rsaEncryptHexByPrivateKey(data, privateKey);
ParameterTypeDescription
dataStringData
privateKeyStringPrivate key

Return Value

Type String, returns the RSA encrypted value of data

rsaEncryptBase64ByPrivateKey

RSA private key encryption (returns Base64 encoded string) (usually used for signing)

javascript
informat.codec.rsaEncryptBase64ByPrivateKey(data, privateKey);
ParameterTypeDescription
dataStringData
privateKeyStringPrivate key

Return Value

Type String, returns the RSA encrypted value of data

rsaEncryptHexByPublicKey

RSA public key encryption (returns hex string) (used for data protection)

javascript
informat.codec.rsaEncryptHexByPublicKey(data, publicKey);
ParameterTypeDescription
dataStringData
publicKeyStringPublic key

Return Value

Type String, returns the RSA encrypted value of data

rsaEncryptBase64ByPublicKey

RSA public key encryption (returns Base64 encoded string) (used for data protection)

javascript
informat.codec.rsaEncryptBase64ByPublicKey(data, publicKey);
ParameterTypeDescription
dataStringData
publicKeyStringPublic key

Return Value

Type String, returns the RSA encrypted value of data

rsaDecryptByPublicKey

RSA public key decryption (usually used for verifying signatures)

javascript
informat.codec.rsaDecryptByPublicKey(data, publicKey);
ParameterTypeDescription
dataStringEncrypted data
publicKeyStringPublic key

Return Value

Type String, returns the RSA decrypted value of encrypted data

rsaDecryptByPrivateKey

RSA private key decryption

javascript
informat.codec.rsaDecryptByPrivateKey(data, privateKey);
ParameterTypeDescription
dataStringEncrypted data
privateKeyStringPrivate key

Return Value

Type String, returns the RSA decrypted value of encrypted data

generateKey

Generate an RSA key pair (containing public and private keys)

javascript
informat.codec.generateKey();

Return Value

Type Map<String, String>, returns a map object containing public and private keys

js
const keyPair = informat.codec.generateKey();
console.log("Public key:", keyPair.publicKeyBase64);
console.log("Private key:", keyPair.privateKeyBase64);