informat.codec Encoding/Decoding
Overview
Use informat.codec to perform encoding, decoding, signing, and other functions.
hash
Get the hash value of a string
informat.codec.hash(str, method);| Parameter | Type | Description |
|---|---|---|
| str | String | String to calculate hash value for |
| method | String | Hash function, optional methods include md5,sha1,sha256,sha224,sha384,sha512 |
Return Value
Type String, returns the hash value of str
Example
informat.codec.hash("hello informat", "md5");
console.log(md5Result);6de76e2f68123fd1a06b86a5dabca022base64Encode
Get the base64 encoding of a string
informat.codec.base64Encode(str);| Parameter | Type | Description |
|---|---|---|
| str | String | String to encode |
Return Value
Type String, returns the base64 value of str
Example
informat.codec.base64Encode("hello informat");aGVsbG8gaW5mb3JtYXQ=base64EncodeToBytes
Get the base64 encoding of a string as bytes
informat.codec.base64EncodeToBytes(str);| Parameter | Type | Description |
|---|---|---|
| str | String | String to encode |
Return Value
Type Array<Byte>, returns the base64 value of str
Example
informat.codec.base64EncodeToBytes("hello informat");"YUdWc2JHOGdhVzVtYjNKdFlYUT0="base64Decode
Return base64 decoded byte array
informat.codec.base64Decode(str);| Parameter | Type | Description |
|---|---|---|
| str | String | Base64 encoded string |
Return Value
Type Array<Byte>, returns the base64 decoded value of str
Example
informat.codec.base64Decode("aGVsbG8gaW5mb3JtYXQ=");'aGVsbG8gaW5mb3JtYXQ='base64DecodeToString
Return base64 decoded string
informat.codec.base64DecodeToString(str);| Parameter | Type | Description |
|---|---|---|
| str | String | Base64 encoded string |
Return Value
Type String, returns the base64 decoded string of str
Example
informat.codec.base64DecodeToString("aGVsbG8gaW5mb3JtYXQ=");'hello informat'base64DecodeFromBytes
Return base64 decoded byte array from bytes
informat.codec.base64DecodeFromBytes(bytes);| Parameter | Type | Description |
|---|---|---|
| bytes | Array<Byte> | Base64 encoded byte array |
Return Value
Type Array<Byte>, returns the base64 decoded value
Example
const encode = informat.codec.base64EncodeToBytes("hello informat");
const decode = informat.codec.base64DecodeFromBytes(encode);aGVsbG8gaW5mb3JtYXQ=sign
Sign a string using a signature algorithm
informat.codec.sign(str, method, privateKey);| Parameter | Type | Description |
|---|---|---|
| str | String | String to sign |
| method | String | Signature method |
| privateKey | String | RSA 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
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);SxWsYRAnMrmKWRW5jEIsI0HiWEXc5Ed6iVC6CZH/t5nCFMu5hRDeJkJW9bpL1SDVJcvMDqKQ9lVuIDutcnote2rU7yxAiOr0pgmBDLjrzPY5eXnsYow/yPL5s8ujhmzrkC9mbjR9tumPHzG2PWqTXX6cxuQ5NjzPqqduDEs0w/g=rsaEncryptHexByPrivateKey
RSA private key encryption (returns hex string) (usually used for signing)
informat.codec.rsaEncryptHexByPrivateKey(data, privateKey);| Parameter | Type | Description |
|---|---|---|
| data | String | Data |
| privateKey | String | Private 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)
informat.codec.rsaEncryptBase64ByPrivateKey(data, privateKey);| Parameter | Type | Description |
|---|---|---|
| data | String | Data |
| privateKey | String | Private key |
Return Value
Type String, returns the RSA encrypted value of data
rsaEncryptHexByPublicKey
RSA public key encryption (returns hex string) (used for data protection)
informat.codec.rsaEncryptHexByPublicKey(data, publicKey);| Parameter | Type | Description |
|---|---|---|
| data | String | Data |
| publicKey | String | Public 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)
informat.codec.rsaEncryptBase64ByPublicKey(data, publicKey);| Parameter | Type | Description |
|---|---|---|
| data | String | Data |
| publicKey | String | Public key |
Return Value
Type String, returns the RSA encrypted value of data
rsaDecryptByPublicKey
RSA public key decryption (usually used for verifying signatures)
informat.codec.rsaDecryptByPublicKey(data, publicKey);| Parameter | Type | Description |
|---|---|---|
| data | String | Encrypted data |
| publicKey | String | Public key |
Return Value
Type String, returns the RSA decrypted value of encrypted data
rsaDecryptByPrivateKey
RSA private key decryption
informat.codec.rsaDecryptByPrivateKey(data, privateKey);| Parameter | Type | Description |
|---|---|---|
| data | String | Encrypted data |
| privateKey | String | Private key |
Return Value
Type String, returns the RSA decrypted value of encrypted data
generateKey
Generate an RSA key pair (containing public and private keys)
informat.codec.generateKey();Return Value
Type Map<String, String>, returns a map object containing public and private keys
const keyPair = informat.codec.generateKey();
console.log("Public key:", keyPair.publicKeyBase64);
console.log("Private key:", keyPair.privateKeyBase64);
