# Mon, 22 Dec 2025 14:57:12 GMT
---
version: 23
data:


- type: ModificationMethod
  attributes:
    displayName: Canadian social insurance number
    description: |
          Generates a Canadian social insurance number (SIN) based on the seed parameter. The generated SIN is returned as a string. Leading zeroes are preserved.
          'RAND' will generate a random value. All other values are used as they are for the seed.
    inputType: COLUMN
    mappingTableContainers: []
    mappingTableAccessMode: READ
    code: |
        /*
         * *****************************************************************************
         * Canadian Social Insurance Number
         * *****************************************************************************
         *
         * The following variables are available in this script:
         * =====================================================
         * ctx             : A context object that offers a number of methods.
         *                   The method indexOf() can be used to get the index of a
         *                   specific column within the dataArray.
         * dataArray       : The data array. This array contains the contents of all
         *                   columns of the current row of the table that is being
         *                   masked by this method.
         *
         * Additionally for column based modification methods the following two
         * variables are available:
         * columnName      : The name of the column that matched the pattern.
         * columnIndex     : The position of the column that matched the pattern within
         *                   the data array. This value is zero-based.
         * *****************************************************************************
         */
        importClass(Packages.de.ubs.xdm3.batch.modification.HashUtils);
        importClass(Packages.de.ubs.xdm3.batch.modification.ModificationException);
        
        /*
         * This function is called once before the processing starts. The method
         * is called once per table.
         */
        function init() {
        
        }
        
        /*
         * This function should modify the row that is provided by the dataArray. The
         * method is called for each single row of the table.
         */
        function apply() {
            var seedValue = "";
        
            if (ctx.indexOf(seed) != -1) {
                seedValue = dataArray[ctx.indexOf(seed)];
            } else {
                if (seed == "RAND") {
                    seedValue = Math.floor(Math.random() * 1000000000);
                } else {
                    seedValue = seed;
                }
            }
        
            var value = new java.lang.String(seedValue).trim();
            if (value.length == 11) {
                var c1 = value.charAt(3);
                var c2 = value.charAt(7);
                if ((c1 == c2) && (c1 == '-' || c1 == ' ' || c1 == '/' || c1 == '.')) {
                    value = value.substring(0, 3) + value.substring(4, 7) + value.substring(8, 11);
                }
            }
            var x = HashUtils.hashStringLikeInt(value);
            x = HashUtils.hashInt(x) % 1000000000;
            var r = HashUtils.luhn(x);
            sin = r.toFixed(0).toString();
            while (sin.length < 9) {
                sin = "0" + sin;
            }
            if ((delimiter != null) && (delimiter != "")) {
                sin = sin.substring(0, 3) + delimiter + sin.substring(3, 6) + delimiter + sin.substring(6, 9);
            }
            dataArray[columnIndex] = sin;
        }
        
        /*
         * This function is executed once after the modification of a table is complete. The method
         * is called once per table.
         */
        function close() {
        }
    parameters:
    - displayName: Seed
      description: |
          Specify a value that is used to calculate the hash.
      variableName: seed
      dataType: STRING
    - displayName: Delimiter
      description: |
          The delimiter character used to separate the groups of digits. May be empty.
      variableName: delimiter
      dataType: STRING
