forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encoding/hash signature to JS files
Reviewed By: tadeuzagallo Differential Revision: D3241028 fb-gh-sync-id: 737ae64cf3a3f4f121076dbfe933d5fe71e07817 fbshipit-source-id: 737ae64cf3a3f4f121076dbfe933d5fe71e07817
- Loading branch information
1 parent
8d3d8b3
commit 34dd17c
Showing
2 changed files
with
37 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
|
||
const createHash = () => crypto.createHash('sha1'); | ||
const isUTF8 = encoding => /^utf-?8$/i.test(encoding); | ||
|
||
exports.appendToString = (string, encoding) => { | ||
const hash = createHash(); | ||
hash.update(string, encoding); | ||
encoding = tryAsciiPromotion(string, encoding); | ||
return string + formatSignature(encoding, hash); | ||
}; | ||
|
||
function tryAsciiPromotion(string, encoding) { | ||
if (!isUTF8(encoding)) { return encoding; } | ||
for (let i = 0, n = string.length; i < n; i++) { | ||
if (string.charCodeAt(i) > 0x7f) { return encoding; } | ||
} | ||
return 'ascii'; | ||
} | ||
|
||
function formatSignature(encoding, hash) { | ||
return `/*${encoding}:${hash.digest('hex')}*/`; | ||
} |