-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Brian DeHamer <[email protected]>
- Loading branch information
Showing
28 changed files
with
448 additions
and
258 deletions.
There are no files selected for viewing
This file contains 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 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 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 GitHub and the TUF Contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,64 @@ | ||
const COMMA = ','; | ||
const COLON = ':'; | ||
const LEFT_SQUARE_BRACKET = '['; | ||
const RIGHT_SQUARE_BRACKET = ']'; | ||
const LEFT_CURLY_BRACKET = '{'; | ||
const RIGHT_CURLY_BRACKET = '}'; | ||
|
||
// Recursively encodes the supplied object according to the canonical JSON form | ||
// as specified at http://wiki.laptop.org/go/Canonical_JSON. It's a restricted | ||
// dialect of JSON in which keys are lexically sorted, floats are not allowed, | ||
// and only double quotes and backslashes are escaped. | ||
function canonicalize(object) { | ||
const buffer = []; | ||
if (typeof object === 'string') { | ||
buffer.push(canonicalizeString(object)); | ||
} else if (typeof object === 'boolean') { | ||
buffer.push(JSON.stringify(object)); | ||
} else if (Number.isInteger(object)) { | ||
buffer.push(JSON.stringify(object)); | ||
} else if (object === null) { | ||
buffer.push(JSON.stringify(object)); | ||
} else if (Array.isArray(object)) { | ||
buffer.push(LEFT_SQUARE_BRACKET); | ||
let first = true; | ||
object.forEach((element) => { | ||
if (!first) { | ||
buffer.push(COMMA); | ||
} | ||
first = false; | ||
buffer.push(canonicalize(element)); | ||
}); | ||
buffer.push(RIGHT_SQUARE_BRACKET); | ||
} else if (typeof object === 'object') { | ||
buffer.push(LEFT_CURLY_BRACKET); | ||
let first = true; | ||
Object.keys(object) | ||
.sort() | ||
.forEach((property) => { | ||
if (!first) { | ||
buffer.push(COMMA); | ||
} | ||
first = false; | ||
buffer.push(canonicalizeString(property)); | ||
buffer.push(COLON); | ||
buffer.push(canonicalize(object[property])); | ||
}); | ||
buffer.push(RIGHT_CURLY_BRACKET); | ||
} else { | ||
throw new TypeError('cannot encode ' + object.toString()); | ||
} | ||
|
||
return buffer.join(''); | ||
} | ||
|
||
// String canonicalization consists of escaping backslash (\) and double | ||
// quote (") characters and wrapping the resulting string in double quotes. | ||
function canonicalizeString(string) { | ||
const escapedString = string.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | ||
return '"' + escapedString + '"'; | ||
} | ||
|
||
module.exports = { | ||
canonicalize, | ||
}; |
This file contains 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,39 @@ | ||
{ | ||
"name": "@tufjs/canonical-json", | ||
"version": "1.0.0", | ||
"description": "OLPC JSON canonicalization", | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"license": "MIT", | ||
"keywords": [ | ||
"json", | ||
"canonical", | ||
"canonicalize", | ||
"canonicalization", | ||
"crypto", | ||
"signature", | ||
"olpc" | ||
], | ||
"author": "[email protected]", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/theupdateframework/tuf-js.git" | ||
}, | ||
"homepage": "https://github.com/theupdateframework/tuf-js/packages/canonical-json#readme", | ||
"bugs": { | ||
"url": "https://github.com/theupdateframework/tuf-js/issues" | ||
}, | ||
"files": [ | ||
"lib/" | ||
], | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.14.1", | ||
"typescript": "^4.9.5" | ||
}, | ||
"engines": { | ||
"node": "^14.17.0 || ^16.13.0 || >=18.0.0" | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 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.