Skip to content
This repository was archived by the owner on Jul 20, 2021. It is now read-only.

Commit bed7495

Browse files
author
Per Kristian Kummermo
committed
feat(base64): allow for decode as hexstring
1 parent 732f497 commit bed7495

5 files changed

Lines changed: 63 additions & 6 deletions

File tree

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,9 @@
6363
"hooks": {
6464
"commit-msg": "validate-commit-msg"
6565
}
66+
},
67+
"dependencies": {
68+
"@types/base64-js": "1.2.5",
69+
"base64-js": "1.3.0"
6670
}
6771
}

src/Config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,22 @@ export enum Base64Action {
3333
DECODE = "decode",
3434
}
3535

36+
/**
37+
* Base64DecodeAs used as config parameter
38+
*/
39+
export enum Base64DecodeAs {
40+
/**
41+
* Config parameter for defining decoding as string
42+
*/
43+
STRING = "string",
44+
/**
45+
* Config parameter for definint decoding as hex string
46+
*/
47+
HEXSTRING = "hexstring",
48+
}
49+
3650
export const Config = {
3751
Base64Action,
52+
Base64DecodeAs,
3853
Endianness,
3954
};

src/mappers/Base64.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { Base64Action } from "../Config";
1+
import { Base64Action, Base64DecodeAs } from "../Config";
22
import { Base64 } from "./Base64";
33

44
describe("Base64 mapper", () => {
55
let base64: Base64;
66
const helloWorldBase64: string = "aGVsbG8gd29ybGQ=";
77
const helloWorldString: string = "hello world";
88

9+
const hexBase64: string = "SAHkwD45wbQ/jbR0Qc5mZkGSiIA/MjQAAAHLAIAAA+K0AAIAAw==";
10+
const hexString: string = "4801e4c03e39c1b43f8db47441ce6666419288803f3234000001cb00800003e2b400020003";
11+
912
beforeEach(() => {
1013
base64 = new Base64();
1114
});
@@ -24,6 +27,15 @@ describe("Base64 mapper", () => {
2427

2528
expect(transformRes).toEqual(helloWorldString);
2629
});
30+
31+
it("should correctly decode a base 64 hexstring given", () => {
32+
base64.action = Base64Action.DECODE;
33+
base64.decodeAs = Base64DecodeAs.HEXSTRING;
34+
35+
const transformRes = base64.transform(hexBase64);
36+
37+
expect(transformRes).toEqual(hexString);
38+
});
2739
});
2840

2941
describe("Configuration", () => {
@@ -36,6 +48,7 @@ describe("Base64 mapper", () => {
3648
id: "BASE64",
3749
params: {
3850
action: Base64Action.ENCODE,
51+
decodeAs: Base64DecodeAs.STRING,
3952
},
4053
});
4154
});

src/mappers/Base64.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { Base64Action } from "../Config";
1+
import * as base64js from "base64-js";
2+
import { Base64Action, Base64DecodeAs } from "../Config";
23
import { IDataValue, IMapper, IMapperConfig, IOutputType } from "./../Typings";
34

45
export interface IBase64Config {
56
action?: Base64Action;
7+
decodeAs?: Base64DecodeAs;
68
}
79

810
export class Base64 implements IMapper {
@@ -13,18 +15,22 @@ export class Base64 implements IMapper {
1315
outputType: IOutputType = IOutputType.string;
1416

1517
action: Base64Action = Base64Action.DECODE;
18+
decodeAs: Base64DecodeAs = Base64DecodeAs.STRING;
1619

1720
constructor({
1821
action = Base64Action.DECODE,
22+
decodeAs = Base64DecodeAs.STRING,
1923
}: IBase64Config = {}) {
2024
this.action = action;
25+
this.decodeAs = decodeAs;
2126
}
2227

2328
config(): IMapperConfig {
2429
return {
2530
id: Base64.id,
2631
params: {
2732
action: this.action,
33+
decodeAs: this.decodeAs,
2834
},
2935
};
3036
}
@@ -34,20 +40,29 @@ export class Base64 implements IMapper {
3440
return data;
3541
}
3642

37-
let resString = data.toString();
43+
let resString: string = data as string;
3844

3945
if (this.action === Base64Action.DECODE) {
4046
try {
41-
resString = window.atob(resString);
47+
const uintArr = base64js.toByteArray(data as string);
48+
49+
if (this.decodeAs === Base64DecodeAs.STRING) {
50+
return String.fromCharCode.apply(null, uintArr as any);
51+
} else if (this.decodeAs === Base64DecodeAs.HEXSTRING) {
52+
return Array.from(uintArr, (byte: number) => {
53+
return ("0" + (byte & 0xFF).toString(16)).slice(-2);
54+
}).join("");
55+
}
4256
} catch (e) {
43-
return data;
57+
return resString;
4458
}
4559
}
60+
4661
if (this.action === Base64Action.ENCODE) {
4762
try {
4863
resString = window.btoa(resString);
4964
} catch (e) {
50-
return data;
65+
return resString;
5166
}
5267
}
5368

0 commit comments

Comments
 (0)