If someone's still interested in this, updated code for latest Nim: 
    
    
    import macros
    
    proc `xor` *(x, y: char): char {.magic: "BitxorI", noSideEffect.}
    
    proc fnv32a*[T: 
string|openArray[char]|openArray[uint8]|openArray[int8]](data: T): int32 =
      result = -18652613'i32
      for b in items(data):
        result = result xor int32(ord(b))
        result = result *% 16777619
    
    proc decodeStr*(s: string, key: int32): string =
      var k = key
      result = newString(s.len)
      for i in s.low .. s.high:
        result[i] = s[i]
        result[i] = result[i] xor chr(uint8(k and 0xFF))
        result[i] = result[i] xor chr(uint8((k shr 8) and 0xFF))
        result[i] = result[i] xor chr(uint8((k shr 16) and 0xFF))
        result[i] = result[i] xor chr(uint8((k shr 24) and 0xFF))
        k = k +% 1
    
    proc encodeStr*(s: string, key: int32): string = decodeStr(s, key)
    
    var encodedCounter {.compiletime.}: int32 = fnv32a(CompileTime & 
CompileDate) and 0x7FFFFFFF
    
    macro xs*(s: untyped): untyped =
      var encodedStr = encodeStr($s, encodedCounter)
      result = quote do:
        decodeStr(`encodedStr`, `encodedCounter`)
      encodedCounter = (encodedCounter *% 16777619) and 0x7FFFFFFF
    
    
    when isMainModule:
      echo xs("invisible")
      echo xs("strings")
    
    
    Run

Reply via email to