<commit_before>'use strict';

/**
 * @ngdoc service
 * @name ubirchAdminCrudApp.NACL
 * @description
 * # NACL
 * Service in the ubirchAdminCrudApp.
 */
angular.module('ubirchAdminCrudApp')
  .factory('NACL', [ 'constants', '$window', 'uuid2', 'moment', 'settings', '$resource',
    function (constants, $window, uuid2, moment, settings, $resource) {
    if(!$window.nacl){
      // TODO: If nacl is not available the user should be redirected to a dedicated error page
      return undefined;
    }

    // encode a number to hex
    function decimalToHex(d, padding) {
      var hex = Number(d).toString(16);
      padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;

      while (hex.length < padding) {
        hex = "0" + hex;
      }

      return hex;
    }

    // encode a string or byte array to hex
    function toHex(s) {
      // utf8 to latin1
      var h = '';
      for (var i = 0; i < s.length; i++) {
        h += decimalToHex(s[i]);
      }
      return h;
    }

    function toCPPCode(x) {
      var r = "";
      for (var i = 0; i < x.length; i += 16) {
        for (var k = 0; k < 16 && k < x.length; k++) {
          r += "0x" + decimalToHex(x[i + k]) + ","
        }
        r += "\n";
      }
      return r;
    }

    var url = settings.UBIRCH_KEY_SERVICE_API_HOST + constants.KEY_SERVICE_REST_ENDPOINT;

    var service = {

    // https://key.dev.ubirch.com/api/keyService/v1/pubkey
      keyservice: $resource(url + '/pubkey'),

      generateKeyPairAndStorePubKey: function (hwDeviceId, callback, errorCallback) {

        var keys = $window.nacl.sign.keyPair();

        if (keys !== undefined){
          var created = moment().utc().format(constants.KEY_TIME_FORMAT);
          var validNotAfter = moment().add(1, 'y').utc().format(constants.KEY_TIME_FORMAT);
          var pubKeyInfo = {
            hwDeviceId: hwDeviceId,
            pubKey: nacl.util.encodeBase64(keys.publicKey),
            pubKeyId: <KEY>(),
            algorithm: "ed25519",
            created: created,
            validNotBefore: created,
            validNotAfter: validNotAfter
          };

          var pubKeyInfoStr = JSON.stringify(pubKeyInfo, Object.keys(pubKeyInfo).sort());
          console.log(pubKeyInfoStr);
          var signature = nacl.sign.detached(nacl.util.decodeUTF8(pubKeyInfoStr), keys.secretKey);

          var payload = {
            pubKeyInfo: pubKeyInfo,
            signature: nacl.util.encodeBase64(signature)
          };
          this.keyservice.save(
            payload,
            function(data){
              if (callback){
                callback(data);
              }
            },
            function (error) {
              if (errorCallback){
                errorCallback(error);
              }
            }
          );
        }
<commit_msg>UBI-364: extract sending pub key to server into separate function<commit_after>'use strict';

/**
 * @ngdoc service
 * @name ubirchAdminCrudApp.NACL
 * @description
 * # NACL
 * Service in the ubirchAdminCrudApp.
 */
angular.module('ubirchAdminCrudApp')
  .factory('NACL', [ 'constants', '$window', 'uuid2', 'moment', 'settings', '$resource',
    function (constants, $window, uuid2, moment, settings, $resource) {
    if(!$window.nacl){
      // TODO: If nacl is not available the user should be redirected to a dedicated error page
      return undefined;
    }

    // encode a number to hex
    function decimalToHex(d, padding) {
      var hex = Number(d).toString(16);
      padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;

      while (hex.length < padding) {
        hex = "0" + hex;
      }

      return hex;
    }

    // encode a string or byte array to hex
    function toHex(s) {
      // utf8 to latin1
      var h = '';
      for (var i = 0; i < s.length; i++) {
        h += decimalToHex(s[i]);
      }
      return h;
    }

    function toCPPCode(x) {
      var r = "";
      for (var i = 0; i < x.length; i += 16) {
        for (var k = 0; k < 16 && k < x.length; k++) {
          r += "0x" + decimalToHex(x[i + k]) + ","
        }
        r += "\n";
      }
      return r;
    }

    function storePubKeyOnServer(keys, callback, errorCallback) {
      var created = moment().utc().format(constants.KEY_TIME_FORMAT);
      var validNotAfter = moment().add(1, 'y').utc().format(constants.KEY_TIME_FORMAT);
      var pubKeyInfo = {
        hwDeviceId: hwDeviceId,
        pubKey: nacl.util.encodeBase64(keys.publicKey),
        pubKeyId: <KEY>(),
        algorithm: "ed25519",
        created: created,
        validNotBefore: created,
        validNotAfter: validNotAfter
      };

      var pubKeyInfoStr = JSON.stringify(pubKeyInfo);
      var signature = nacl.sign.detached(nacl.util.decodeUTF8(pubKeyInfoStr), keys.secretKey);

      var payload = {
        pubKeyInfo: pubKeyInfo,
        signature: nacl.util.encodeBase64(signature)
      };
      this.keyservice.save(
        payload,
        function(data){
          if (callback){
            callback(data);
          }
        },
        function (error) {
          if (errorCallback){
            errorCallback(error);
          }
        }
      );
    }

    var url = settings.UBIRCH_KEY_SERVICE_API_HOST + constants.KEY_SERVICE_REST_ENDPOINT;

    var service = {

    // https://key.dev.ubirch.com/api/keyService/v1/pubkey
      keyservice: $resource(url + '/pubkey'),

      generateKeyPairAndStorePubKey: function (hwDeviceId, callback, errorCallback) {

        var keys = $window.nacl.sign.keyPair();

        if (keys !== undefined){
          storePubKeyOnServer(keys, callback, errorCallback);
        }

{'fraction_non_alphanumeric': 0.13227792436235708, 'fraction_numerical': 0.010378188214599824, 'mean_word_length': 2.076839826839827, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6584693', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1739, 'n_words': 412}