1 /*! nodeutil-1.0.0 (c) 2015 Kenji Urushima | kjur.github.com/jsrsasign/license 2 */ 3 /* 4 * nodeutil.js - Utilities for Node 5 * 6 * version: 1.0.0 (2015 Nov 11) 7 * 8 * Copyright (c) 2015 Kenji Urushima (kenji.urushima@gmail.com) 9 * 10 * This software is licensed under the terms of the MIT License. 11 * http://kjur.github.com/jsrsasign/license/ 12 * 13 * The above copyright and license notice shall be 14 * included in all copies or substantial portions of the Software. 15 */ 16 17 /** 18 * @fileOverview 19 * @name nodeutil-1.0.js 20 * @author Kenji Urushima kenji.urushima@gmail.com 21 * @version 1.0.0 (2015-Nov-11) 22 * @since jsrsasign 5.0.2 23 * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a> 24 */ 25 26 /** 27 * read file and return file contents as utf-8 string 28 * @param {String} utf8File file name to be read 29 * @return {String} utf-8 string of file contents 30 * @description 31 * This function only works in Node.js. 32 */ 33 function readFileUTF8(utf8File) { 34 return require('fs').readFileSync(utf8File, 'utf8'); 35 } 36 37 /** 38 * read binary file and return file contents as hexadecimal string 39 * @param {String} binFile file name to be read 40 * @return {String} hexadecimal string of file contents 41 * @description 42 * This function only works in Node.js. 43 */ 44 function readFileHexByBin(binFile) { 45 var rs = require('jsrsasign'); 46 var fs = require('fs'); 47 return rs.rstrtohex(fs.readFileSync(binFile, 'binary')); 48 } 49 50 /** 51 * read file and return file contents 52 * @param {String} binFile file name to be read 53 * @return {String} raw string of file contents 54 * @description 55 * This function only works in Node.js. 56 */ 57 function readFile(binFile) { 58 var fs = require('fs'); 59 return fs.readFileSync(binFile, 'binary'); 60 } 61 62 /** 63 * save raw string to file 64 * @param {String} binFile file name to save contents. 65 * @param {String} rawString string contents to be saved. 66 * @description 67 * This function only works in Node.js. 68 */ 69 function saveFile(binFile, rawString) { 70 var fs = require('fs'); 71 fs.writeFileSync(binFile, rawString, 'binary'); 72 } 73 74 /** 75 * save data represented by hexadecimal string to file 76 * @param {String} binFile file name to save contents. 77 * @param {String} hexString hexadecimal string to be saved. 78 * @description 79 * This function only works in Node.js. 80 */ 81 function saveFileBinByHex(binFile, hexString) { 82 var fs = require('fs'); 83 var rs = require('jsrsasign'); 84 var rawString = rs.hextorstr(hexString); 85 fs.writeFileSync(binFile, rawString, 'binary'); 86 } 87