-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Introduce Yubico's Modhex for Conversion #1105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /** | ||
| * @author linuxgemini [ilteris@asenkron.com.tr] | ||
| * @copyright Crown Copyright 2024 | ||
| * @license Apache-2.0 | ||
| */ | ||
|
|
||
| import Utils from "../Utils.mjs"; | ||
| import OperationError from "../errors/OperationError.mjs"; | ||
| import { fromHex, toHex } from "./Hex.mjs"; | ||
|
|
||
| /** | ||
| * Modhex alphabet. | ||
| */ | ||
| const MODHEX_ALPHABET = "cbdefghijklnrtuv"; | ||
|
|
||
|
|
||
| /** | ||
| * Modhex alphabet map. | ||
| */ | ||
| const MODHEX_ALPHABET_MAP = MODHEX_ALPHABET.split(""); | ||
|
|
||
|
|
||
| /** | ||
| * Hex alphabet to substitute Modhex. | ||
| */ | ||
| const HEX_ALPHABET = "0123456789abcdef"; | ||
|
|
||
|
|
||
| /** | ||
| * Hex alphabet map to substitute Modhex. | ||
| */ | ||
| const HEX_ALPHABET_MAP = HEX_ALPHABET.split(""); | ||
|
|
||
|
|
||
| /** | ||
| * Convert a byte array into a modhex string. | ||
| * | ||
| * @param {byteArray|Uint8Array|ArrayBuffer} data | ||
| * @param {string} [delim=" "] | ||
| * @param {number} [padding=2] | ||
| * @returns {string} | ||
| * | ||
| * @example | ||
| * // returns "cl bf bu" | ||
| * toModhex([10,20,30]); | ||
| * | ||
| * // returns "cl:bf:bu" | ||
| * toModhex([10,20,30], ":"); | ||
| */ | ||
| export function toModhex(data, delim=" ", padding=2, extraDelim="", lineSize=0) { | ||
linuxgemini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!data) return ""; | ||
| if (data instanceof ArrayBuffer) data = new Uint8Array(data); | ||
|
|
||
| const regularHexString = toHex(data, "", padding, "", 0); | ||
|
|
||
| let modhexString = ""; | ||
| for (const letter of regularHexString.split("")) { | ||
| modhexString += MODHEX_ALPHABET_MAP[HEX_ALPHABET_MAP.indexOf(letter)]; | ||
| } | ||
|
|
||
| let output = ""; | ||
| const groupingRegexp = new RegExp(`.{1,${padding}}`, "g"); | ||
| const groupedModhex = modhexString.match(groupingRegexp); | ||
|
|
||
| for (let i = 0; i < groupedModhex.length; i++) { | ||
| const group = groupedModhex[i]; | ||
| output += group + delim; | ||
|
|
||
| if (extraDelim) { | ||
| output += extraDelim; | ||
| } | ||
| // Add LF after each lineSize amount of bytes but not at the end | ||
| if ((i !== groupedModhex.length - 1) && ((i + 1) % lineSize === 0)) { | ||
| output += "\n"; | ||
| } | ||
| } | ||
|
|
||
| // Remove the extraDelim at the end (if there is one) | ||
| // and remove the delim at the end, but if it's prepended there's nothing to remove | ||
| const rTruncLen = extraDelim.length + delim.length; | ||
| if (rTruncLen) { | ||
| // If rTruncLen === 0 then output.slice(0,0) will be returned, which is nothing | ||
| return output.slice(0, -rTruncLen); | ||
| } else { | ||
| return output; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Convert a byte array into a modhex string as efficiently as possible with no options. | ||
| * | ||
| * @param {byteArray|Uint8Array|ArrayBuffer} data | ||
| * @returns {string} | ||
| * | ||
| * @example | ||
| * // returns "clbfbu" | ||
| * toModhexFast([10,20,30]); | ||
| */ | ||
| export function toModhexFast(data) { | ||
| if (!data) return ""; | ||
| if (data instanceof ArrayBuffer) data = new Uint8Array(data); | ||
|
|
||
| const output = []; | ||
|
|
||
| for (let i = 0; i < data.length; i++) { | ||
| output.push(MODHEX_ALPHABET_MAP[(data[i] >> 4) & 0xf]); | ||
| output.push(MODHEX_ALPHABET_MAP[data[i] & 0xf]); | ||
| } | ||
| return output.join(""); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Convert a modhex string into a byte array. | ||
| * | ||
| * @param {string} data | ||
| * @param {string} [delim] | ||
| * @param {number} [byteLen=2] | ||
| * @returns {byteArray} | ||
| * | ||
| * @example | ||
| * // returns [10,20,30] | ||
| * fromModhex("cl bf bu"); | ||
| * | ||
| * // returns [10,20,30] | ||
| * fromModhex("cl:bf:bu", "Colon"); | ||
| */ | ||
| export function fromModhex(data, delim="Auto", byteLen=2) { | ||
linuxgemini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (byteLen < 1 || Math.round(byteLen) !== byteLen) | ||
| throw new OperationError("Byte length must be a positive integer"); | ||
|
|
||
| // The `.replace(/\s/g, "")` an interesting workaround: Hex "multiline" tests aren't actually | ||
| // multiline. Tests for Modhex fixes that, thus exposing the issue. | ||
| data = data.toLowerCase().replace(/\s/g, ""); | ||
|
|
||
| if (delim !== "None") { | ||
| const delimRegex = delim === "Auto" ? /[^cbdefghijklnrtuv]/gi : Utils.regexRep(delim); | ||
| data = data.split(delimRegex); | ||
| } else { | ||
| data = [data]; | ||
| } | ||
|
|
||
| let regularHexString = ""; | ||
| for (let i = 0; i < data.length; i++) { | ||
| for (const letter of data[i].split("")) { | ||
| regularHexString += HEX_ALPHABET_MAP[MODHEX_ALPHABET_MAP.indexOf(letter)]; | ||
| } | ||
| } | ||
|
|
||
| const output = fromHex(regularHexString, "None", byteLen); | ||
| return output; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * To Modhex delimiters. | ||
| */ | ||
| export const TO_MODHEX_DELIM_OPTIONS = ["Space", "Percent", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "None"]; | ||
|
|
||
|
|
||
| /** | ||
| * From Modhex delimiters. | ||
| */ | ||
| export const FROM_MODHEX_DELIM_OPTIONS = ["Auto"].concat(TO_MODHEX_DELIM_OPTIONS); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * @author linuxgemini [ilteris@asenkron.com.tr] | ||
| * @copyright Crown Copyright 2024 | ||
| * @license Apache-2.0 | ||
| */ | ||
|
|
||
| import Operation from "../Operation.mjs"; | ||
| import { FROM_MODHEX_DELIM_OPTIONS, fromModhex } from "../lib/Modhex.mjs"; | ||
|
|
||
| /** | ||
| * From Modhex operation | ||
| */ | ||
| class FromModhex extends Operation { | ||
|
|
||
| /** | ||
| * FromModhex constructor | ||
| */ | ||
| constructor() { | ||
| super(); | ||
|
|
||
| this.name = "From Modhex"; | ||
| this.module = "Default"; | ||
| this.description = "Converts a modhex byte string back into its raw value."; | ||
| this.infoURL = "https://en.wikipedia.org/wiki/YubiKey#ModHex"; | ||
| this.inputType = "string"; | ||
| this.outputType = "byteArray"; | ||
| this.args = [ | ||
| { | ||
| name: "Delimiter", | ||
| type: "option", | ||
| value: FROM_MODHEX_DELIM_OPTIONS | ||
| } | ||
| ]; | ||
| this.checks = [ | ||
| { | ||
| pattern: "^(?:[cbdefghijklnrtuv]{2})+$", | ||
| flags: "i", | ||
| args: ["None"] | ||
| }, | ||
| { | ||
| pattern: "^[cbdefghijklnrtuv]{2}(?: [cbdefghijklnrtuv]{2})*$", | ||
| flags: "i", | ||
| args: ["Space"] | ||
| }, | ||
| { | ||
| pattern: "^[cbdefghijklnrtuv]{2}(?:,[cbdefghijklnrtuv]{2})*$", | ||
| flags: "i", | ||
| args: ["Comma"] | ||
| }, | ||
| { | ||
| pattern: "^[cbdefghijklnrtuv]{2}(?:;[cbdefghijklnrtuv]{2})*$", | ||
| flags: "i", | ||
| args: ["Semi-colon"] | ||
| }, | ||
| { | ||
| pattern: "^[cbdefghijklnrtuv]{2}(?::[cbdefghijklnrtuv]{2})*$", | ||
| flags: "i", | ||
| args: ["Colon"] | ||
| }, | ||
| { | ||
| pattern: "^[cbdefghijklnrtuv]{2}(?:\\n[cbdefghijklnrtuv]{2})*$", | ||
| flags: "i", | ||
| args: ["Line feed"] | ||
| }, | ||
| { | ||
| pattern: "^[cbdefghijklnrtuv]{2}(?:\\r\\n[cbdefghijklnrtuv]{2})*$", | ||
| flags: "i", | ||
| args: ["CRLF"] | ||
| } | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} input | ||
| * @param {Object[]} args | ||
| * @returns {byteArray} | ||
| */ | ||
| run(input, args) { | ||
| const delim = args[0] || "Auto"; | ||
| return fromModhex(input, delim, 2); | ||
| } | ||
| } | ||
|
|
||
| export default FromModhex; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * @author linuxgemini [ilteris@asenkron.com.tr] | ||
| * @copyright Crown Copyright 2024 | ||
| * @license Apache-2.0 | ||
| */ | ||
|
|
||
| import Operation from "../Operation.mjs"; | ||
| import { TO_MODHEX_DELIM_OPTIONS, toModhex } from "../lib/Modhex.mjs"; | ||
| import Utils from "../Utils.mjs"; | ||
|
|
||
| /** | ||
| * To Modhex operation | ||
| */ | ||
| class ToModhex extends Operation { | ||
|
|
||
| /** | ||
| * ToModhex constructor | ||
| */ | ||
| constructor() { | ||
| super(); | ||
|
|
||
| this.name = "To Modhex"; | ||
| this.module = "Default"; | ||
| this.description = "Converts the input string to modhex bytes separated by the specified delimiter."; | ||
| this.infoURL = "https://en.wikipedia.org/wiki/YubiKey#ModHex"; | ||
| this.inputType = "ArrayBuffer"; | ||
| this.outputType = "string"; | ||
| this.args = [ | ||
| { | ||
| name: "Delimiter", | ||
| type: "option", | ||
| value: TO_MODHEX_DELIM_OPTIONS | ||
| }, | ||
| { | ||
| name: "Bytes per line", | ||
| type: "number", | ||
| value: 0 | ||
| } | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param {ArrayBuffer} input | ||
| * @param {Object[]} args | ||
| * @returns {string} | ||
| */ | ||
| run(input, args) { | ||
| const delim = Utils.charRep(args[0]); | ||
| const lineSize = args[1]; | ||
|
|
||
| return toModhex(new Uint8Array(input), delim, 2, "", lineSize); | ||
| } | ||
| } | ||
|
|
||
| export default ToModhex; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.