diff --git a/.gitignore b/.gitignore index 356e055..8ff7975 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # User Specific Project Files *.pro.user *.pro.user.* +rpm/harbour-sailotp.spec diff --git a/README.md b/README.md index 43776f8..237f292 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,16 @@ The item can be unstared by tapping the star icon again on the main view. From the main view a token can be copied to the clipboard by tapping on it. +From the pulley menu the token database can be exported to a file for backup purposes. The backup is a AES-256-CBC encrypted and +Base64 encoded file containing a JSON-representation of the database. It can be decrypted with openssl using the following command: + + openssl enc -d -a -aes-256-cbc -in + +if you need the information outside of SailOTP. + +Importing the file is also possible from the pulley menu. If a file contains tokens that are already in the database +(title and secret of the token match an existing one), these tokens will not be added again. + ## Known Limitations At the moment the only way to insert new entries into the app is to insert the @@ -43,6 +53,10 @@ SailOTP uses the SHA-1 and HMAC-Implementation from https://github.com/Caligatio/jsSHA +SailOTP also uses the AES-Implementation from + +https://github.com/mdp/gibberish-aes + The implementation of the TOTP-algorithm was inspired by: http://blog.tinisles.com/2011/10/google-authenticator-one-time-password-algorithm-in-javascript/ diff --git a/harbour-sailotp.pro b/harbour-sailotp.pro index bc48895..c8511ac 100644 --- a/harbour-sailotp.pro +++ b/harbour-sailotp.pro @@ -8,9 +8,13 @@ # - icon definition filename in desktop file must be changed TARGET = harbour-sailotp +DEFINES += APP_VERSION=\\\"$$VERSION\\\" +DEFINES += APP_BUILDNUM=\\\"$$RELEASE\\\" + CONFIG += sailfishapp -SOURCES += src/harbour-sailotp.cpp +SOURCES += src/harbour-sailotp.cpp \ + src/fileio.cpp OTHER_FILES += qml/harbour-sailotp.qml \ qml/cover/CoverPage.qml \ @@ -23,5 +27,11 @@ OTHER_FILES += qml/harbour-sailotp.qml \ qml/lib/storage.js \ qml/lib/crypto.js \ qml/lib/sha.js \ - qml/sailotp.png + qml/sailotp.png \ + qml/pages/ExportPage.qml \ + qml/lib/gibberish-aes.js \ + qml/components/NotifyBanner.qml + +HEADERS += \ + src/fileio.h diff --git a/harbour-sailotp.pro.user b/harbour-sailotp.pro.user deleted file mode 100644 index 8537e8a..0000000 --- a/harbour-sailotp.pro.user +++ /dev/null @@ -1,529 +0,0 @@ - - - - - - ProjectExplorer.Project.ActiveTarget - 1 - - - ProjectExplorer.Project.EditorSettings - - true - false - true - - Cpp - - CppGlobal - - - - QmlJS - - QmlJSGlobal - - - 2 - UTF-8 - false - 4 - false - true - 1 - true - 0 - true - 0 - 8 - true - 1 - true - true - true - false - - - - ProjectExplorer.Project.PluginSettings - - - - ProjectExplorer.Project.Target.0 - - MerSDK-SailfishOS-armv7hl - MerSDK-SailfishOS-armv7hl - {45efb901-3e2e-4d50-95ba-c432b8781a69} - 0 - 0 - 0 - - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - true - - false - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - false - - - - 2 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - true - clean - - - 1 - Bereinigen - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Debug - - Qt4ProjectManager.Qt4BuildConfiguration - 2 - /home/seiichiro/Projekte/Sailfish/build-harbour-sailotp-MerSDK_SailfishOS_armv7hl-Debug - true - - - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - true - - false - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - false - - - - 2 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - true - clean - - - 1 - Bereinigen - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Release - - Qt4ProjectManager.Qt4BuildConfiguration - 0 - /home/seiichiro/Projekte/Sailfish/build-harbour-sailotp-MerSDK_SailfishOS_armv7hl-Release - true - - 2 - - - - true - Rpm - - Qt4ProjectManager.MerRpmBuildStep - - 1 - Deployment - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy By Building An RPM Package - - Qt4ProjectManager.MerArmDeployConfiguration - - - - - true - Rsync - - Qt4ProjectManager.MerRsyncDeployStep - - 1 - Deployment - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy By Copying Binaries - - Qt4ProjectManager.MerRSyncDeployConfiguration - - - - - true - Rpm - - Qt4ProjectManager.MerRpmDeployStep - - 1 - Deployment - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy As RPM Package - - Qt4ProjectManager.MerRpmDeployConfiguration - - 3 - - - true - - false - false - false - false - true - 0.01 - 10 - true - 25 - - true - valgrind - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - - 1 - - harbour-sailotp (auf Mobilgerät) - - Qt4ProjectManager.MerRunConfiguration:/home/seiichiro/Projekte/Sailfish/harbour-sailotp/harbour-sailotp.pro - - harbour-sailotp.pro - - false - - 3768 - true - false - false - true - false - - 1 - - - - ProjectExplorer.Project.Target.1 - - MerSDK-SailfishOS-i486-x86 - MerSDK-SailfishOS-i486-x86 - {08e6ddb6-6d52-4f4e-938d-998fac04baad} - 0 - 0 - 0 - - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - true - - false - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - false - - - - 2 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - true - clean - - - 1 - Bereinigen - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Debug - - Qt4ProjectManager.Qt4BuildConfiguration - 2 - /home/seiichiro/Projekte/Sailfish/build-harbour-sailotp-MerSDK_SailfishOS_i486_x86-Debug - true - - - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - true - - false - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - false - - - - 2 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - -w - -r - - true - clean - - - 1 - Bereinigen - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Release - - Qt4ProjectManager.Qt4BuildConfiguration - 0 - /home/seiichiro/Projekte/Sailfish/build-harbour-sailotp-MerSDK_SailfishOS_i486_x86-Release - true - - 2 - - - - true - Start Emulator - - Qt4ProjectManager.MerEmulatorStartStep - - - true - Rsync - - Qt4ProjectManager.MerRsyncDeployStep - - 2 - Deployment - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy By Copying Binaries - - Qt4ProjectManager.MerRSyncDeployConfiguration - - - - - true - Start Emulator - - Qt4ProjectManager.MerEmulatorStartStep - - - true - Rpm - - Qt4ProjectManager.MerRpmDeployStep - - 2 - Deployment - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy As RPM Package - - Qt4ProjectManager.MerRpmDeployConfiguration - - 2 - - - true - - false - false - false - false - true - 0.01 - 10 - true - 25 - - true - valgrind - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - - 1 - - harbour-sailotp (auf Mobilgerät) - - Qt4ProjectManager.MerRunConfiguration:/home/seiichiro/Projekte/Sailfish/harbour-sailotp/harbour-sailotp.pro - - harbour-sailotp.pro - - false - - 3768 - true - false - false - true - false - - 1 - - - - ProjectExplorer.Project.TargetCount - 2 - - - ProjectExplorer.Project.Updater.EnvironmentId - {ea1259ba-c840-46cd-876a-b91d58edff1f} - - - ProjectExplorer.Project.Updater.FileVersion - 14 - - diff --git a/qml/components/NotifyBanner.qml b/qml/components/NotifyBanner.qml new file mode 100644 index 0000000..3274ec3 --- /dev/null +++ b/qml/components/NotifyBanner.qml @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2013, Stefan Brand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. The names of the contributors may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import QtQuick 2.0 +import Sailfish.Silica 1.0 + +MouseArea { + id: notifyBanner + width: Screen.width + height: notifyText.height + 2*Theme.paddingMedium + visible: showBanner + + property bool showBanner: false + + function show(text, time) { + notifyText.text = text; + timeout.interval = time; + showBanner = true; + timeout.start(); + } + + Timer { + id: timeout + interval: 3000 + onTriggered: { + interval = 3000 + showBanner = false + } + } + + Rectangle { + id: banner + anchors.fill: parent + color: Theme.secondaryHighlightColor + + Text { + id: notifyText + anchors.left: parent.left + anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter + anchors.margins: Theme.paddingLarge + + + font.pixelSize: Theme.fontSizeSmall + color: Theme.primaryColor + wrapMode: Text.Wrap + elide: Text.ElideRight + maximumLineCount: 3 + } + } + + onClicked: { + showBanner = false + timeout.stop() + timeout.interval = 3000 + } +} diff --git a/qml/harbour-sailotp.qml b/qml/harbour-sailotp.qml index db90a21..fb555f1 100644 --- a/qml/harbour-sailotp.qml +++ b/qml/harbour-sailotp.qml @@ -30,6 +30,7 @@ import QtQuick 2.0 import Sailfish.Silica 1.0 import "pages" +import "components" ApplicationWindow { @@ -41,6 +42,8 @@ ApplicationWindow property string coverType: "" property string coverOTP: "------" + NotifyBanner { id: notify } + initialPage: Component { MainView { } } cover: Qt.resolvedUrl("cover/CoverPage.qml") } diff --git a/qml/lib/gibberish-aes.js b/qml/lib/gibberish-aes.js new file mode 100644 index 0000000..99b0d2d --- /dev/null +++ b/qml/lib/gibberish-aes.js @@ -0,0 +1,1008 @@ +/** +* @license Gibberish-AES +* A lightweight Javascript Libray for OpenSSL compatible AES CBC encryption. +* +* Author: Mark Percival +* Email: mark@mpercival.com +* Copyright: Mark Percival - http://mpercival.com 2008 +* +* With thanks to: +* Josh Davis - http://www.josh-davis.org/ecmaScrypt +* Chris Veness - http://www.movable-type.co.uk/scripts/aes.html +* Michel I. Gallant - http://www.jensign.com/ +* Jean-Luc Cooke 2012-07-12: added strhex + invertArr to compress G2X/G3X/G9X/GBX/GEX/SBox/SBoxInv/Rcon saving over 7KB, and added encString, decString, also made the MD5 routine more easlier compressible using yuicompressor. +* +* License: MIT +* +* Usage: GibberishAES.enc("secret", "password") +* Outputs: AES Encrypted text encoded in Base64 +*/ +var AES + +(function (root, factory) { + AES = factory(); +}(this, function () { + 'use strict'; + + var Nr = 14, + /* Default to 256 Bit Encryption */ + Nk = 8, + Decrypt = false, + + enc_utf8 = function(s) + { + try { + return unescape(encodeURIComponent(s)); + } + catch(e) { + throw 'Error on UTF-8 encode'; + } + }, + + dec_utf8 = function(s) + { + try { + return decodeURIComponent(escape(s)); + } + catch(e) { + throw ('Bad Key'); + } + }, + + padBlock = function(byteArr) + { + var array = [], cpad, i; + if (byteArr.length < 16) { + cpad = 16 - byteArr.length; + array = [cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad, cpad]; + } + for (i = 0; i < byteArr.length; i++) + { + array[i] = byteArr[i]; + } + return array; + }, + + block2s = function(block, lastBlock) + { + var string = '', padding, i; + if (lastBlock) { + padding = block[15]; + if (padding > 16) { + throw ('Decryption error: Maybe bad key'); + } + if (padding === 16) { + return ''; + } + for (i = 0; i < 16 - padding; i++) { + string += String.fromCharCode(block[i]); + } + } else { + for (i = 0; i < 16; i++) { + string += String.fromCharCode(block[i]); + } + } + return string; + }, + + a2h = function(numArr) + { + var string = '', i; + for (i = 0; i < numArr.length; i++) { + string += (numArr[i] < 16 ? '0': '') + numArr[i].toString(16); + } + return string; + }, + + h2a = function(s) + { + var ret = []; + s.replace(/(..)/g, + function(s) { + ret.push(parseInt(s, 16)); + }); + return ret; + }, + + s2a = function(string, binary) { + var array = [], i; + + if (! binary) { + string = enc_utf8(string); + } + + for (i = 0; i < string.length; i++) + { + array[i] = string.charCodeAt(i); + } + + return array; + }, + + size = function(newsize) + { + switch (newsize) + { + case 128: + Nr = 10; + Nk = 4; + break; + case 192: + Nr = 12; + Nk = 6; + break; + case 256: + Nr = 14; + Nk = 8; + break; + default: + throw ('Invalid Key Size Specified:' + newsize); + } + }, + + randArr = function(num) { + var result = [], i; + for (i = 0; i < num; i++) { + result = result.concat(Math.floor(Math.random() * 256)); + } + return result; + }, + + openSSLKey = function(passwordArr, saltArr) { + // Number of rounds depends on the size of the AES in use + // 3 rounds for 256 + // 2 rounds for the key, 1 for the IV + // 2 rounds for 128 + // 1 round for the key, 1 round for the IV + // 3 rounds for 192 since it's not evenly divided by 128 bits + var rounds = Nr >= 12 ? 3: 2, + key = [], + iv = [], + md5_hash = [], + result = [], + data00 = passwordArr.concat(saltArr), + i; + md5_hash[0] = MD5(data00); + result = md5_hash[0]; + for (i = 1; i < rounds; i++) { + md5_hash[i] = MD5(md5_hash[i - 1].concat(data00)); + result = result.concat(md5_hash[i]); + } + key = result.slice(0, 4 * Nk); + iv = result.slice(4 * Nk, 4 * Nk + 16); + return { + key: key, + iv: iv + }; + }, + + rawEncrypt = function(plaintext, key, iv) { + // plaintext, key and iv as byte arrays + key = expandKey(key); + var numBlocks = Math.ceil(plaintext.length / 16), + blocks = [], + i, + cipherBlocks = []; + for (i = 0; i < numBlocks; i++) { + blocks[i] = padBlock(plaintext.slice(i * 16, i * 16 + 16)); + } + if (plaintext.length % 16 === 0) { + blocks.push([16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16]); + // CBC OpenSSL padding scheme + numBlocks++; + } + for (i = 0; i < blocks.length; i++) { + blocks[i] = (i === 0) ? xorBlocks(blocks[i], iv) : xorBlocks(blocks[i], cipherBlocks[i - 1]); + cipherBlocks[i] = encryptBlock(blocks[i], key); + } + return cipherBlocks; + }, + + rawDecrypt = function(cryptArr, key, iv, binary) { + // cryptArr, key and iv as byte arrays + key = expandKey(key); + var numBlocks = cryptArr.length / 16, + cipherBlocks = [], + i, + plainBlocks = [], + string = ''; + for (i = 0; i < numBlocks; i++) { + cipherBlocks.push(cryptArr.slice(i * 16, (i + 1) * 16)); + } + for (i = cipherBlocks.length - 1; i >= 0; i--) { + plainBlocks[i] = decryptBlock(cipherBlocks[i], key); + plainBlocks[i] = (i === 0) ? xorBlocks(plainBlocks[i], iv) : xorBlocks(plainBlocks[i], cipherBlocks[i - 1]); + } + for (i = 0; i < numBlocks - 1; i++) { + string += block2s(plainBlocks[i]); + } + string += block2s(plainBlocks[i], true); + return binary ? string : dec_utf8(string); + }, + + encryptBlock = function(block, words) { + Decrypt = false; + var state = addRoundKey(block, words, 0), + round; + for (round = 1; round < (Nr + 1); round++) { + state = subBytes(state); + state = shiftRows(state); + if (round < Nr) { + state = mixColumns(state); + } + //last round? don't mixColumns + state = addRoundKey(state, words, round); + } + + return state; + }, + + decryptBlock = function(block, words) { + Decrypt = true; + var state = addRoundKey(block, words, Nr), + round; + for (round = Nr - 1; round > -1; round--) { + state = shiftRows(state); + state = subBytes(state); + state = addRoundKey(state, words, round); + if (round > 0) { + state = mixColumns(state); + } + //last round? don't mixColumns + } + + return state; + }, + + subBytes = function(state) { + var S = Decrypt ? SBoxInv: SBox, + temp = [], + i; + for (i = 0; i < 16; i++) { + temp[i] = S[state[i]]; + } + return temp; + }, + + shiftRows = function(state) { + var temp = [], + shiftBy = Decrypt ? [0, 13, 10, 7, 4, 1, 14, 11, 8, 5, 2, 15, 12, 9, 6, 3] : [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11], + i; + for (i = 0; i < 16; i++) { + temp[i] = state[shiftBy[i]]; + } + return temp; + }, + + mixColumns = function(state) { + var t = [], + c; + if (!Decrypt) { + for (c = 0; c < 4; c++) { + t[c * 4] = G2X[state[c * 4]] ^ G3X[state[1 + c * 4]] ^ state[2 + c * 4] ^ state[3 + c * 4]; + t[1 + c * 4] = state[c * 4] ^ G2X[state[1 + c * 4]] ^ G3X[state[2 + c * 4]] ^ state[3 + c * 4]; + t[2 + c * 4] = state[c * 4] ^ state[1 + c * 4] ^ G2X[state[2 + c * 4]] ^ G3X[state[3 + c * 4]]; + t[3 + c * 4] = G3X[state[c * 4]] ^ state[1 + c * 4] ^ state[2 + c * 4] ^ G2X[state[3 + c * 4]]; + } + }else { + for (c = 0; c < 4; c++) { + t[c*4] = GEX[state[c*4]] ^ GBX[state[1+c*4]] ^ GDX[state[2+c*4]] ^ G9X[state[3+c*4]]; + t[1+c*4] = G9X[state[c*4]] ^ GEX[state[1+c*4]] ^ GBX[state[2+c*4]] ^ GDX[state[3+c*4]]; + t[2+c*4] = GDX[state[c*4]] ^ G9X[state[1+c*4]] ^ GEX[state[2+c*4]] ^ GBX[state[3+c*4]]; + t[3+c*4] = GBX[state[c*4]] ^ GDX[state[1+c*4]] ^ G9X[state[2+c*4]] ^ GEX[state[3+c*4]]; + } + } + + return t; + }, + + addRoundKey = function(state, words, round) { + var temp = [], + i; + for (i = 0; i < 16; i++) { + temp[i] = state[i] ^ words[round][i]; + } + return temp; + }, + + xorBlocks = function(block1, block2) { + var temp = [], + i; + for (i = 0; i < 16; i++) { + temp[i] = block1[i] ^ block2[i]; + } + return temp; + }, + + expandKey = function(key) { + // Expects a 1d number array + var w = [], + temp = [], + i, + r, + t, + flat = [], + j; + + for (i = 0; i < Nk; i++) { + r = [key[4 * i], key[4 * i + 1], key[4 * i + 2], key[4 * i + 3]]; + w[i] = r; + } + + for (i = Nk; i < (4 * (Nr + 1)); i++) { + w[i] = []; + for (t = 0; t < 4; t++) { + temp[t] = w[i - 1][t]; + } + if (i % Nk === 0) { + temp = subWord(rotWord(temp)); + temp[0] ^= Rcon[i / Nk - 1]; + } else if (Nk > 6 && i % Nk === 4) { + temp = subWord(temp); + } + for (t = 0; t < 4; t++) { + w[i][t] = w[i - Nk][t] ^ temp[t]; + } + } + for (i = 0; i < (Nr + 1); i++) { + flat[i] = []; + for (j = 0; j < 4; j++) { + flat[i].push(w[i * 4 + j][0], w[i * 4 + j][1], w[i * 4 + j][2], w[i * 4 + j][3]); + } + } + return flat; + }, + + subWord = function(w) { + // apply SBox to 4-byte word w + for (var i = 0; i < 4; i++) { + w[i] = SBox[w[i]]; + } + return w; + }, + + rotWord = function(w) { + // rotate 4-byte word w left by one byte + var tmp = w[0], + i; + for (i = 0; i < 4; i++) { + w[i] = w[i + 1]; + } + w[3] = tmp; + return w; + }, + +// jlcooke: 2012-07-12: added strhex + invertArr to compress G2X/G3X/G9X/GBX/GEX/SBox/SBoxInv/Rcon saving over 7KB, and added encString, decString + strhex = function(str,size) { + var i, ret = []; + for (i=0; i0x7f) ? 0x11b^(a<<1) : (a<<1); + b >>>= 1; + } + + return ret; + }, + Gx = function(x) { + var i, r = []; + for (i=0; i<256; i++){ + r[i] = Gxx(x, i); + } + return r; + }, + + // S-box +/* + SBox = [ + 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, + 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, + 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, + 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, + 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, + 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, + 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, + 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, + 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, + 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, + 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, + 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, + 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, + 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, + 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, + 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, + 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, + 22], //*/ SBox = strhex('637c777bf26b6fc53001672bfed7ab76ca82c97dfa5947f0add4a2af9ca472c0b7fd9326363ff7cc34a5e5f171d8311504c723c31896059a071280e2eb27b27509832c1a1b6e5aa0523bd6b329e32f8453d100ed20fcb15b6acbbe394a4c58cfd0efaafb434d338545f9027f503c9fa851a3408f929d38f5bcb6da2110fff3d2cd0c13ec5f974417c4a77e3d645d197360814fdc222a908846eeb814de5e0bdbe0323a0a4906245cc2d3ac629195e479e7c8376d8dd54ea96c56f4ea657aae08ba78252e1ca6b4c6e8dd741f4bbd8b8a703eb5664803f60e613557b986c11d9ee1f8981169d98e949b1e87e9ce5528df8ca1890dbfe6426841992d0fb054bb16',2), + + // Precomputed lookup table for the inverse SBox +/* SBoxInv = [ + 82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, + 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, + 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, + 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, + 109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, + 204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21, + 70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247, + 228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2, + 193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220, + 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173, + 53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29, + 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75, + 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168, + 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81, + 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160, + 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97, + 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, + 125], //*/ SBoxInv = invertArr(SBox), + + // Rijndael Rcon +/* + Rcon = [1, 2, 4, 8, 16, 32, 64, 128, 27, 54, 108, 216, 171, 77, 154, 47, 94, + 188, 99, 198, 151, 53, 106, 212, 179, 125, 250, 239, 197, 145], +//*/ Rcon = strhex('01020408102040801b366cd8ab4d9a2f5ebc63c697356ad4b37dfaefc591',2), + +/* + G2X = [ + 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, + 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, + 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40, 0x42, 0x44, 0x46, + 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, + 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, + 0x78, 0x7a, 0x7c, 0x7e, 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, + 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, 0xa0, 0xa2, 0xa4, 0xa6, + 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, + 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, + 0xd8, 0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, + 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, 0x1b, 0x19, 0x1f, 0x1d, + 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, + 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, + 0x23, 0x21, 0x27, 0x25, 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, + 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, 0x7b, 0x79, 0x7f, 0x7d, + 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 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bd, + 0xb4, 0xb7, 0xb2, 0xb1, 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, + 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, 0x9b, 0x98, 0x9d, 0x9e, + 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, + 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, + 0xbf, 0xbc, 0xb9, 0xba, 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, + 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, 0xcb, 0xc8, 0xcd, 0xce, + 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, + 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, + 0x4f, 0x4c, 0x49, 0x4a, 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, + 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, 0x3b, 0x38, 0x3d, 0x3e, + 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, + 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, + 0x1f, 0x1c, 0x19, 0x1a + ], //*/ G3X = Gx(3), + +/* + G9X = [ + 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, + 0x6c, 0x65, 0x7e, 0x77, 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, + 0xd8, 0xd1, 0xca, 0xc3, 0xfc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, 0xea, + 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, + 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, + 0x33, 0x38, 0x25, 0x2e, 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, + 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, 0x3c, 0x37, 0x2a, 0x21, + 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, + 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, + 0x75, 0x7e, 0x63, 0x68, 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, + 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, 0x7a, 0x71, 0x6c, 0x67, + 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, + 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, + 0xbe, 0xb5, 0xa8, 0xa3 + ], //*/ GBX = Gx(0xb), + +/* + GDX = [ + 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, + 0x5c, 0x51, 0x46, 0x4b, 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, + 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, 0xbb, 0xb6, 0xa1, 0xac, + 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, + 0x6b, 0x66, 0x71, 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, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, + 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, + 0x80, 0x8d, 0x9a, 0x97 + ], //*/ GDX = Gx(0xd), + +/* + GEX = [ + 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, + 0x48, 0x46, 0x54, 0x5a, 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, + 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, 0xdb, 0xd5, 0xc7, 0xc9, + 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, + 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, + 0x73, 0x7d, 0x6f, 0x61, 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, + 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, 0x4d, 0x43, 0x51, 0x5f, + 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, + 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, + 0x3e, 0x30, 0x22, 0x2c, 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, + 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, 0x41, 0x4f, 0x5d, 0x53, + 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, + 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, + 0xe9, 0xe7, 0xf5, 0xfb, 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, + 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, 0x7a, 0x74, 0x66, 0x68, + 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, + 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, + 0xa4, 0xaa, 0xb8, 0xb6, 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, + 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, 0x37, 0x39, 0x2b, 0x25, + 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, + 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, + 0x9f, 0x91, 0x83, 0x8d + ], //*/ GEX = Gx(0xe), + + enc = function(string, pass, binary) { + // string, password in plaintext + var salt = randArr(8), + pbe = openSSLKey(s2a(pass, binary), salt), + key = pbe.key, + iv = pbe.iv, + cipherBlocks, + saltBlock = [[83, 97, 108, 116, 101, 100, 95, 95].concat(salt)]; + string = s2a(string, binary); + cipherBlocks = rawEncrypt(string, key, iv); + // Spells out 'Salted__' + cipherBlocks = saltBlock.concat(cipherBlocks); + return Base64.encode(cipherBlocks); + }, + + dec = function(string, pass, binary) { + // string, password in plaintext + var cryptArr = Base64.decode(string), + salt = cryptArr.slice(8, 16), + pbe = openSSLKey(s2a(pass, binary), salt), + key = pbe.key, + iv = pbe.iv; + cryptArr = cryptArr.slice(16, cryptArr.length); + // Take off the Salted__ffeeddcc + string = rawDecrypt(cryptArr, key, iv, binary); + return string; + }, + + MD5 = function(numArr) { + + function rotateLeft(lValue, iShiftBits) { + return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits)); + } + + function addUnsigned(lX, lY) { + var lX4, + lY4, + lX8, + lY8, + lResult; + lX8 = (lX & 0x80000000); + lY8 = (lY & 0x80000000); + lX4 = (lX & 0x40000000); + lY4 = (lY & 0x40000000); + lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF); + if (lX4 & lY4) { + return (lResult ^ 0x80000000 ^ lX8 ^ lY8); + } + if (lX4 | lY4) { + if (lResult & 0x40000000) { + return (lResult ^ 0xC0000000 ^ lX8 ^ lY8); + } else { + return (lResult ^ 0x40000000 ^ lX8 ^ lY8); + } + } else { + return (lResult ^ lX8 ^ lY8); + } + } + + function f(x, y, z) { + return (x & y) | ((~x) & z); + } + function g(x, y, z) { + return (x & z) | (y & (~z)); + } + function h(x, y, z) { + return (x ^ y ^ z); + } + function funcI(x, y, z) { + return (y ^ (x | (~z))); + } + + function ff(a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(f(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); + } + + function gg(a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(g(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); + } + + function hh(a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(h(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); + } + + function ii(a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(funcI(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); + } + + function convertToWordArray(numArr) { + var lWordCount, + lMessageLength = numArr.length, + lNumberOfWords_temp1 = lMessageLength + 8, + lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64, + lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16, + lWordArray = [], + lBytePosition = 0, + lByteCount = 0; + while (lByteCount < lMessageLength) { + lWordCount = (lByteCount - (lByteCount % 4)) / 4; + lBytePosition = (lByteCount % 4) * 8; + lWordArray[lWordCount] = (lWordArray[lWordCount] | (numArr[lByteCount] << lBytePosition)); + lByteCount++; + } + lWordCount = (lByteCount - (lByteCount % 4)) / 4; + lBytePosition = (lByteCount % 4) * 8; + lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition); + lWordArray[lNumberOfWords - 2] = lMessageLength << 3; + lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29; + return lWordArray; + } + + function wordToHex(lValue) { + var lByte, + lCount, + wordToHexArr = []; + for (lCount = 0; lCount <= 3; lCount++) { + lByte = (lValue >>> (lCount * 8)) & 255; + wordToHexArr = wordToHexArr.concat(lByte); + } + return wordToHexArr; + } + + /*function utf8Encode(string) { + string = string.replace(/\r\n/g, "\n"); + var utftext = "", + n, + c; + + for (n = 0; n < string.length; n++) { + + c = string.charCodeAt(n); + + if (c < 128) { + utftext += String.fromCharCode(c); + } + else if ((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192); + utftext += String.fromCharCode((c & 63) | 128); + } + else { + utftext += String.fromCharCode((c >> 12) | 224); + utftext += String.fromCharCode(((c >> 6) & 63) | 128); + utftext += String.fromCharCode((c & 63) | 128); + } + + } + + return utftext; + }*/ + + var x = [], + k, + AA, + BB, + CC, + DD, + a, + b, + c, + d, + rnd = strhex('67452301efcdab8998badcfe10325476d76aa478e8c7b756242070dbc1bdceeef57c0faf4787c62aa8304613fd469501698098d88b44f7afffff5bb1895cd7be6b901122fd987193a679438e49b40821f61e2562c040b340265e5a51e9b6c7aad62f105d02441453d8a1e681e7d3fbc821e1cde6c33707d6f4d50d87455a14eda9e3e905fcefa3f8676f02d98d2a4c8afffa39428771f6816d9d6122fde5380ca4beea444bdecfa9f6bb4b60bebfbc70289b7ec6eaa127fad4ef308504881d05d9d4d039e6db99e51fa27cf8c4ac5665f4292244432aff97ab9423a7fc93a039655b59c38f0ccc92ffeff47d85845dd16fa87e4ffe2ce6e0a30143144e0811a1f7537e82bd3af2352ad7d2bbeb86d391',8); + + x = convertToWordArray(numArr); + + a = rnd[0]; + b = rnd[1]; + c = rnd[2]; + d = rnd[3]; + + for (k = 0; k < x.length; k += 16) { + AA = a; + BB = b; + CC = c; + DD = d; + a = ff(a, b, c, d, x[k + 0], 7, rnd[4]); + d = ff(d, a, b, c, x[k + 1], 12, rnd[5]); + c = ff(c, d, a, b, x[k + 2], 17, rnd[6]); + b = ff(b, c, d, a, x[k + 3], 22, rnd[7]); + a = ff(a, b, c, d, x[k + 4], 7, rnd[8]); + d = ff(d, a, b, c, x[k + 5], 12, rnd[9]); + c = ff(c, d, a, b, x[k + 6], 17, rnd[10]); + b = ff(b, c, d, a, x[k + 7], 22, rnd[11]); + a = ff(a, b, c, d, x[k + 8], 7, rnd[12]); + d = ff(d, a, b, c, x[k + 9], 12, rnd[13]); + c = ff(c, d, a, b, x[k + 10], 17, rnd[14]); + b = ff(b, c, d, a, x[k + 11], 22, rnd[15]); + a = ff(a, b, c, d, x[k + 12], 7, rnd[16]); + d = ff(d, a, b, c, x[k + 13], 12, rnd[17]); + c = ff(c, d, a, b, x[k + 14], 17, rnd[18]); + b = ff(b, c, d, a, x[k + 15], 22, rnd[19]); + a = gg(a, b, c, d, x[k + 1], 5, rnd[20]); + d = gg(d, a, b, c, x[k + 6], 9, rnd[21]); + c = gg(c, d, a, b, x[k + 11], 14, rnd[22]); + b = gg(b, c, d, a, x[k + 0], 20, rnd[23]); + a = gg(a, b, c, d, x[k + 5], 5, rnd[24]); + d = gg(d, a, b, c, x[k + 10], 9, rnd[25]); + c = gg(c, d, a, b, x[k + 15], 14, rnd[26]); + b = gg(b, c, d, a, x[k + 4], 20, rnd[27]); + a = gg(a, b, c, d, x[k + 9], 5, rnd[28]); + d = gg(d, a, b, c, x[k + 14], 9, rnd[29]); + c = gg(c, d, a, b, x[k + 3], 14, rnd[30]); + b = gg(b, c, d, a, x[k + 8], 20, rnd[31]); + a = gg(a, b, c, d, x[k + 13], 5, rnd[32]); + d = gg(d, a, b, c, x[k + 2], 9, rnd[33]); + c = gg(c, d, a, b, x[k + 7], 14, rnd[34]); + b = gg(b, c, d, a, x[k + 12], 20, rnd[35]); + a = hh(a, b, c, d, x[k + 5], 4, rnd[36]); + d = hh(d, a, b, c, x[k + 8], 11, rnd[37]); + c = hh(c, d, a, b, x[k + 11], 16, rnd[38]); + b = hh(b, c, d, a, x[k + 14], 23, rnd[39]); + a = hh(a, b, c, d, x[k + 1], 4, rnd[40]); + d = hh(d, a, b, c, x[k + 4], 11, rnd[41]); + c = hh(c, d, a, b, x[k + 7], 16, rnd[42]); + b = hh(b, c, d, a, x[k + 10], 23, rnd[43]); + a = hh(a, b, c, d, x[k + 13], 4, rnd[44]); + d = hh(d, a, b, c, x[k + 0], 11, rnd[45]); + c = hh(c, d, a, b, x[k + 3], 16, rnd[46]); + b = hh(b, c, d, a, x[k + 6], 23, rnd[47]); + a = hh(a, b, c, d, x[k + 9], 4, rnd[48]); + d = hh(d, a, b, c, x[k + 12], 11, rnd[49]); + c = hh(c, d, a, b, x[k + 15], 16, rnd[50]); + b = hh(b, c, d, a, x[k + 2], 23, rnd[51]); + a = ii(a, b, c, d, x[k + 0], 6, rnd[52]); + d = ii(d, a, b, c, x[k + 7], 10, rnd[53]); + c = ii(c, d, a, b, x[k + 14], 15, rnd[54]); + b = ii(b, c, d, a, x[k + 5], 21, rnd[55]); + a = ii(a, b, c, d, x[k + 12], 6, rnd[56]); + d = ii(d, a, b, c, x[k + 3], 10, rnd[57]); + c = ii(c, d, a, b, x[k + 10], 15, rnd[58]); + b = ii(b, c, d, a, x[k + 1], 21, rnd[59]); + a = ii(a, b, c, d, x[k + 8], 6, rnd[60]); + d = ii(d, a, b, c, x[k + 15], 10, rnd[61]); + c = ii(c, d, a, b, x[k + 6], 15, rnd[62]); + b = ii(b, c, d, a, x[k + 13], 21, rnd[63]); + a = ii(a, b, c, d, x[k + 4], 6, rnd[64]); + d = ii(d, a, b, c, x[k + 11], 10, rnd[65]); + c = ii(c, d, a, b, x[k + 2], 15, rnd[66]); + b = ii(b, c, d, a, x[k + 9], 21, rnd[67]); + a = addUnsigned(a, AA); + b = addUnsigned(b, BB); + c = addUnsigned(c, CC); + d = addUnsigned(d, DD); + } + + return wordToHex(a).concat(wordToHex(b), wordToHex(c), wordToHex(d)); + }, + + encString = function(plaintext, key, iv) { + var i; + plaintext = s2a(plaintext); + + key = s2a(key); + for (i=key.length; i<32; i++){ + key[i] = 0; + } + + if (iv === undefined) { + // TODO: This is not defined anywhere... commented out... + // iv = genIV(); + } else { + iv = s2a(iv); + for (i=iv.length; i<16; i++){ + iv[i] = 0; + } + } + + var ct = rawEncrypt(plaintext, key, iv); + var ret = [iv]; + for (i=0; i> 2]; + b64 += chars[((flatArr[i] & 3) << 4) | (flatArr[i + 1] >> 4)]; + if ( flatArr[i + 1] !== undefined ) { + b64 += chars[((flatArr[i + 1] & 15) << 2) | (flatArr[i + 2] >> 6)]; + } else { + b64 += '='; + } + if ( flatArr[i + 2] !== undefined ) { + b64 += chars[flatArr[i + 2] & 63]; + } else { + b64 += '='; + } + } + // OpenSSL is super particular about line breaks + broken_b64 = b64.slice(0, 64) + '\n'; + for (i = 1; i < (Math.ceil(b64.length / 64)); i++) { + broken_b64 += b64.slice(i * 64, i * 64 + 64) + (Math.ceil(b64.length / 64) === i + 1 ? '': '\n'); + } + return broken_b64; + }, + + decode = function(string) { + string = string.replace(/\n/g, ''); + var flatArr = [], + c = [], + b = [], + i; + for (i = 0; i < string.length; i = i + 4) { + c[0] = _chars.indexOf(string.charAt(i)); + c[1] = _chars.indexOf(string.charAt(i + 1)); + c[2] = _chars.indexOf(string.charAt(i + 2)); + c[3] = _chars.indexOf(string.charAt(i + 3)); + + b[0] = (c[0] << 2) | (c[1] >> 4); + b[1] = ((c[1] & 15) << 4) | (c[2] >> 2); + b[2] = ((c[2] & 3) << 6) | c[3]; + flatArr.push(b[0], b[1], b[2]); + } + flatArr = flatArr.slice(0, flatArr.length - (flatArr.length % 16)); + return flatArr; + }; + + //internet explorer + if(typeof Array.indexOf === "function") { + _chars = chars; + } + + /* + //other way to solve internet explorer problem + if(!Array.indexOf){ + Array.prototype.indexOf = function(obj){ + for(var i=0; i 0) { + return(JSON.stringify({"app": "sailotp", "version": 1, "otplist": otpList})); + } else { + return("") + } +} + +// Read Values from JSON and put them into the DB +function json2db(jsonString, error) { + var json = JSON.parse(jsonString); + error = ""; + + if (json.version != "1" && json.app != "sailotp" ) { + error = "Unrecognized format, file is not a SailOTP export"; + return(false); + } else { + var otpList = []; + otpList = json.otplist; + if (otpList.length > 0) { + while(otpList.length > 0) { + var otpItem = otpList.shift(); + if (otpItem.title != "" & otpItem.secret.length >= 16) { + addOTP(otpItem.title, otpItem.secret, otpItem.type, otpItem.counter); + } + } + parentPage.refreshOTPList(); + return(true); + } else { + error = "File contains no Tokens"; + return(false); + } + } } // Add a new OTP @@ -80,9 +129,26 @@ function addOTP(title, secret, type, counter) { db.transaction( function(tx) { - tx.executeSql("INSERT INTO OTPStorage VALUES(?, ?, ?, ?, ?);", [title, secret, type, counter, 0]); - } - ) + if (checkOTP(title, secret)) { + console.log("Token " + title + " is already in DB"); + } else { + tx.executeSql("INSERT INTO OTPStorage VALUES(?, ?, ?, ?, ?);", [title, secret, type, counter, 0]); + console.log("Token " + title + " added."); + } + }); +} + +// Check if an OTP Token already exists in the DB +function checkOTP(title, secret) { + var db = getDB(); + var res + + db.transaction( + function(tx) { + res = tx.executeSql("select title FROM OTPStorage WHERE title=? and secret=?;", [title, secret]); + }); + + return res.rows.length > 0 ? true : false } // Remove an existing OTP @@ -92,10 +158,10 @@ function removeOTP(title, secret) { db.transaction( function(tx) { tx.executeSql("DELETE FROM OTPStorage WHERE title=? and secret=?;", [title, secret]); - } - ) + }); } +// Set OTP to favourite function setFav(title, secret) { var db = getDB(); @@ -103,18 +169,17 @@ function setFav(title, secret) { function(tx) { tx.executeSql("UPDATE OTPStorage set fav = 0"); tx.executeSql("UPDATE OTPStorage set fav = 1 WHERE title=? and secret=?;", [title, secret]); - } - ) + }); } +// Reset favourite Flag for OTP function resetFav(title, secret) { var db = getDB(); db.transaction( function(tx) { tx.executeSql("UPDATE OTPStorage set fav = 0"); - } - ) + }); } // Change an existing OTP @@ -123,12 +188,15 @@ function changeOTP(title, secret, type, counter, oldtitle, oldsecret) { db.transaction( function(tx) { - tx.executeSql("UPDATE OTPStorage SET title=?, secret=?, type=?, counter=? WHERE title=? and secret=?;", [title, secret, type, counter, oldtitle, oldsecret]); - } - ) + if (checkOTP(title, secret)) { + console.log("Token " + title + " is already in DB"); + } else { + tx.executeSql("UPDATE OTPStorage SET title=?, secret=?, type=?, counter=? WHERE title=? and secret=?;", [title, secret, type, counter, oldtitle, oldsecret]); + console.log("Token " + title + " modified."); + } + }); } - // Get the counter for a HOTP value, incerment the counter on request function getCounter(title, secret, increment) { var db = getDB(); @@ -139,8 +207,7 @@ function getCounter(title, secret, increment) { function(tx) { res = tx.executeSql("SELECT counter FROM OTPStorage where title=? and secret=?;", [title, secret]); if (increment) tx.executeSql("UPDATE OTPStorage set counter=counter+1 WHERE title=? and secret=?;", [title, secret]); - } - ) + }); return res.rows.item(0).counter; } diff --git a/qml/pages/About.qml b/qml/pages/About.qml index d2dcf17..7fe3b02 100644 --- a/qml/pages/About.qml +++ b/qml/pages/About.qml @@ -44,7 +44,7 @@ Page { anchors.horizontalCenter: parent.horizontalCenter y: 320 font.bold: true - text: "SailOTP 0.4" + text: "SailOTP " + Qt.application.version } Text { id: desc @@ -67,17 +67,21 @@ Page { anchors.horizontalCenter: parent.horizontalCenter anchors.top: copyright.bottom anchors.topMargin: 20 - text: "SailOTP on Github" + text: "Source Code" onClicked: { - Qt.openUrlExternally("https://github.com/seiichiro0185/sailotp") + Qt.openUrlExternally("https://www.seiichiro0185.org/gitlab/seiichiro0185/harbour-sailotp.git") } } - Text { - id: accnowledgement + TextArea { + id: acknowledgement anchors.horizontalCenter: parent.horizontalCenter anchors.top: homepage.bottom anchors.topMargin: 20 - text: "SailOTP uses the SHA-1 Implementation
from http://caligatio.github.io/jsSHA/" + width: parent.width + font.pixelSize: Theme.fontSizeSmall + horizontalAlignment: TextEdit.Center + readOnly: true + text: "SailOTP uses the following third party libs:\n\nhttp://caligatio.github.io/jsSHA/\nhttps://github.com/mdp/gibberish-aes" color: "white" } } diff --git a/qml/pages/ExportPage.qml b/qml/pages/ExportPage.qml new file mode 100644 index 0000000..f496e70 --- /dev/null +++ b/qml/pages/ExportPage.qml @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2013, Stefan Brand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. The names of the contributors may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +import QtQuick 2.0 +import Sailfish.Silica 1.0 +import harbour.sailotp.FileIO 1.0 // Import FileIO Class +import "../lib/storage.js" as DB // Import the storage library for Config-Access +import "../lib/gibberish-aes.js" as Gibberish //Import AES encryption library + +// Define Layout of the Export / Import Page +Dialog { + id: exportPage + + // We get the Object of the parent page on call to refresh it after adding a new Entry + property QtObject parentPage: null + property string mode: "import" + + function fillNum(num) { + if (num < 10) { + return("0"+num); + } else { + return(num) + } + } + + function creFileName() { + var date = new Date(); + return("/home/nemo/sailotp_"+date.getFullYear()+fillNum(date.getMonth()+1)+fillNum(date.getDate())+".aes"); + } + + function checkFileName(file) { + if (mode == "export") { + if (exportFile.exists(file) && !fileOverwrite.checked) { + notify.show("File already exists, choose \"Overwrite existing\" to overwrite it.", 4000); + return(false) + } else { + return(true) + } + } else { + if (exportFile.exists(file)) { + return(true) + } else { + notify.show("Given file does not exist!", 4000); + return(false) + } + } + } + + // FileIO Object for reading / writing files + FileIO { + id: exportFile + source: fileName.text + onError: { console.log(msg); } + } + + SilicaFlickable { + id: exportFlickable + anchors.fill: parent + + VerticalScrollDecorator {} + + Column { + anchors.fill: parent + DialogHeader { + acceptText: mode == "export" ? "Export" : "Import" + } + + TextField { + id: fileName + width: parent.width + text: mode == "export" ? creFileName() : "/home/nemo/"; + label: "Filename" + placeholderText: mode == "import" ? "File to import" : "File to export" + focus: true + horizontalAlignment: TextInput.AlignLeft + } + + TextSwitch { + id: fileOverwrite + checked: false + visible: mode == "export" + text: "Overwrite existing" + } + + TextField { + id: filePassword + width: parent.width + label: "Password" + placeholderText: "Password for the file" + echoMode: TextInput.Password + focus: true + horizontalAlignment: TextInput.AlignLeft + } + + TextField { + id: filePasswordCheck + width: parent.width + label: (filePassword.text != filePasswordCheck.text && filePassword.text.length > 0) ? "Passwords don't match!" : "Passwords match!" + placeholderText: "Repeated Password for the file" + visible: mode == "export" + echoMode: TextInput.Password + focus: true + horizontalAlignment: TextInput.AlignLeft + } + + Text { + id: importText + + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottomMargin: 20 + width: parent.width - 2*Theme.paddingLarge + + wrapMode: Text.Wrap + maximumLineCount: 15 + font.pixelSize: Theme.fontSizeSmall + color: Theme.secondaryColor + + visible: mode == "import" + text: "Here you can Import Tokens from a file. Put in the file location and the password you used on export. Pull left to start the import." + } + + Text { + id: exportText + + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottomMargin: 20 + width: parent.width - 2*Theme.paddingLarge + + wrapMode: Text.Wrap + maximumLineCount: 15 + font.pixelSize: Theme.fontSizeSmall + color: Theme.secondaryColor + + visible: mode == "export" + text: "Here you can export Tokens to a file. The exported file will be encrypted with AES-256-CBC and Base64 encoded. Choose a strong password, the file will contain the secrets used to generate the Tokens for your accounts. Pull left to start the export." + } + } + } + + // Check if we can continue + canAccept: fileName.text.length > 0 && filePassword.text.length > 0 && (mode == "import" || filePassword.text == filePasswordCheck.text) && checkFileName(fileName.text) ? true : false + + // Do the DB-Export / Import + onDone: { + if (result == DialogResult.Accepted) { + var plainText = "" + var chipherText = "" + + if (mode == "export") { + // Export Database to File + plainText = DB.db2json(); + + if (plainText != "") { + try { + chipherText = Gibberish.AES.enc(plainText, filePassword.text); + if (!exportFile.write(chipherText)) { + notify.show("Error writing to file "+ fileName.text, 4000); + } else { + notify.show("Token Database exported to "+ fileName.text, 4000); + } + } catch(e) { + notify.show("Could not encrypt tokens. Error: ", 4000); + } + } else { + notify.show("Could not read tokens from Database", 4000); + } + } else if(mode == "import") { + // Import Tokens from File + + chipherText = exportFile.read(); + if (chipherText != "") { + try { + var errormsg = "" + plainText = Gibberish.AES.dec(chipherText, filePassword.text); + if (DB.json2db(plainText, errormsg)) { + notify.show("Tokens imported from "+ fileName.text, 4000); + } else { + notify.show(errormsg, 4000); + } + } catch (e) { + notify.show("Unable to decrypt file, did you use the right password?", 4000); + } + } else { + notify.show("Could not read from file " + fileName.text, 4000); + } + } + } + } +} diff --git a/qml/pages/MainView.qml b/qml/pages/MainView.qml index eb46901..7849f89 100644 --- a/qml/pages/MainView.qml +++ b/qml/pages/MainView.qml @@ -5,25 +5,25 @@ * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * - * 1. Redistributions of source code must retain the above copyright notice, this + * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other + * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. - * - * 3. The names of the contributors may not be used to endorse or promote products + * + * 3. The names of the contributors may not be used to endorse or promote products * derived from this software without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ @@ -40,7 +40,7 @@ Page { id: otpListModel } - // This holds the time of the last update of the page as Unix Timestamp (in Milliseconds) + // This holds the time of the last update of the page as Unix Timestamp (in Milliseconds) property double lastUpdated: 0 // Add an entry to the list @@ -71,11 +71,11 @@ Page { // Calculate new OTPs for every entry function refreshOTPValues() { - // get seconds from current Date - var curDate = new Date(); + // get seconds from current Date + var curDate = new Date(); var seconds = curDate.getSeconds(); - // Iterate over all List entries + // Iterate over all List entries for (var i=0; i> macros -# << macros - -%{!?qtc_qmake:%define qtc_qmake %qmake} -%{!?qtc_qmake5:%define qtc_qmake5 %qmake5} -%{!?qtc_make:%define qtc_make make} -%{?qtc_builddir:%define _builddir %qtc_builddir} -Summary: SailOTP -Version: 0.2 -Release: 1 -Group: Security -License: BSD -URL: https://github.com/seiichiro0185/sailotp/ -Source0: %{name}-%{version}.tar.bz2 -Source100: harbour-sailotp.yaml -Requires: sailfishsilica-qt5 >= 0.10.9 -BuildRequires: pkgconfig(sailfishapp) >= 0.0.10 -BuildRequires: pkgconfig(Qt5Core) -BuildRequires: pkgconfig(Qt5Qml) -BuildRequires: pkgconfig(Qt5Quick) -BuildRequires: desktop-file-utils - -%description -A Sailfish implementation of the Timebased One Time Pad algorithm as used by Google Authenticator and a growing number of Websites. - - -%prep -%setup -q -n %{name}-%{version} - -# >> setup -# << setup - -%build -# >> build pre -# << build pre - -%qtc_qmake5 - -%qtc_make %{?_smp_mflags} - -# >> build post -# << build post - -%install -rm -rf %{buildroot} -# >> install pre -# << install pre -%qmake5_install - -# >> install post -# << install post - -desktop-file-install --delete-original \ - --dir %{buildroot}%{_datadir}/applications \ - %{buildroot}%{_datadir}/applications/*.desktop - -%files -%defattr(-,root,root,-) -/usr/share/icons/hicolor/86x86/apps -/usr/share/applications -/usr/share/harbour-sailotp -/usr/bin -%{_datadir}/icons/hicolor/86x86/apps/%{name}.png -%{_datadir}/applications/%{name}.desktop -%{_datadir}/%{name}/qml -%{_bindir} -# >> files -# << files diff --git a/rpm/harbour-sailotp.yaml b/rpm/harbour-sailotp.yaml index a529c1c..92484d7 100644 --- a/rpm/harbour-sailotp.yaml +++ b/rpm/harbour-sailotp.yaml @@ -1,7 +1,7 @@ Name: harbour-sailotp Summary: SailOTP -Version: 0.4 -Release: 2 +Version: 0.5 +Release: 1 Group: Security URL: https://github.com/seiichiro0185/sailotp/ License: "BSD\t" @@ -11,20 +11,23 @@ Description: | A Sailfish implementation of the One Time Pad algorithm as used by Google Authenticator and a growing number of Websites. Configure: none Builder: qtc5 +QMakeOptions: +- VERSION=%{version} +- RELEASE=%{release} PkgConfigBR: -- sailfishapp >= 0.0.10 -- Qt5Core -- Qt5Qml - Qt5Quick +- Qt5Qml +- Qt5Core +- sailfishapp >= 0.0.10 Requires: - sailfishsilica-qt5 >= 0.10.9 Files: -- /usr/share/icons/hicolor/86x86/apps -- /usr/share/applications -- /usr/share/harbour-sailotp -- /usr/bin -- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png' -- '%{_datadir}/applications/%{name}.desktop' -- '%{_datadir}/%{name}/qml' - '%{_bindir}' +- '%{_datadir}/%{name}/qml' +- '%{_datadir}/applications/%{name}.desktop' +- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png' +- /usr/bin +- /usr/share/harbour-sailotp +- /usr/share/applications +- /usr/share/icons/hicolor/86x86/apps PkgBR: [] diff --git a/src/fileio.cpp b/src/fileio.cpp new file mode 100644 index 0000000..9793d60 --- /dev/null +++ b/src/fileio.cpp @@ -0,0 +1,73 @@ +#include "fileio.h" +#include +#include + +FileIO::FileIO(QObject *parent) : + QObject(parent) +{ + +} + +QString FileIO::read() +{ + if (mSource.isEmpty()){ + emit error("source is empty"); + return QString(); + } + + QFile file(mSource); + QString fileContent; + if ( file.open(QIODevice::ReadOnly) ) { + QString line; + QTextStream t( &file ); + do { + line = t.readLine(); + fileContent += line; + } while (!line.isNull()); + + file.close(); + } else { + emit error("Unable to open the file"); + return QString(); + } + return fileContent; +} + +bool FileIO::write(const QString& data) +{ + if (mSource.isEmpty()) + return false; + + QFile file(mSource); + if (!file.open(QFile::WriteOnly | QFile::Truncate)) + return false; + + QTextStream out(&file); + out << data; + + file.close(); + + return true; +} + +bool FileIO::exists() +{ + if (mSource.isEmpty()) { + emit error("Source is empty!"); + return false; + } + + QFile file(mSource); + return file.exists(); +} + +bool FileIO::exists(const QString& filename) +{ + if (filename.isEmpty()) { + emit error("Source is empty!"); + return false; + } + + QFile file(filename); + return file.exists(); +} diff --git a/src/fileio.h b/src/fileio.h new file mode 100644 index 0000000..e8c67c2 --- /dev/null +++ b/src/fileio.h @@ -0,0 +1,35 @@ +#ifndef FILEIO_H +#define FILEIO_H + +#include + +class FileIO : public QObject +{ + Q_OBJECT + +public: + Q_PROPERTY(QString source + READ source + WRITE setSource + NOTIFY sourceChanged) + explicit FileIO(QObject *parent = 0); + + Q_INVOKABLE QString read(); + Q_INVOKABLE bool write(const QString& data); + Q_INVOKABLE bool exists(); + Q_INVOKABLE bool exists(const QString& filename); + + QString source() { return mSource; }; + +public slots: + void setSource(const QString& source) { mSource = source; }; + +signals: + void sourceChanged(const QString& source); + void error(const QString& msg); + +private: + QString mSource; +}; + +#endif // FILEIO_H diff --git a/src/harbour-sailotp.cpp b/src/harbour-sailotp.cpp index f6d8d32..16a6177 100644 --- a/src/harbour-sailotp.cpp +++ b/src/harbour-sailotp.cpp @@ -27,14 +27,26 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef QT_QML_DEBUG #include -#endif - #include +#include +#include "fileio.h" int main(int argc, char *argv[]) { - return SailfishApp::main(argc, argv); + QScopedPointer app(SailfishApp::application(argc, argv)); + QScopedPointer view(SailfishApp::createView()); + + app->setOrganizationName("harbour-sailotp"); + app->setOrganizationDomain("harbour-sailotp"); + app->setApplicationName("harbour-sailotp"); + app->setApplicationVersion(APP_VERSION); + + qmlRegisterType("harbour.sailotp.FileIO", 1, 0, "FileIO"); + + view->setSource(SailfishApp::pathTo("qml/harbour-sailotp.qml")); + view->show(); + + return app->exec(); }