Simple obfuscation routines
(plaintext string)
| 313 | |
| 314 | // Simple obfuscation routines |
| 315 | func (c *Cipher) obfuscateSegment(plaintext string) string { |
| 316 | if plaintext == "" { |
| 317 | return "" |
| 318 | } |
| 319 | |
| 320 | // If the string isn't valid UTF8 then don't rotate; just |
| 321 | // prepend a !. |
| 322 | if !utf8.ValidString(plaintext) { |
| 323 | return "!." + plaintext |
| 324 | } |
| 325 | |
| 326 | // Calculate a simple rotation based on the filename and |
| 327 | // the nameKey |
| 328 | var dir int |
| 329 | for _, runeValue := range plaintext { |
| 330 | dir += int(runeValue) |
| 331 | } |
| 332 | dir %= 256 |
| 333 | |
| 334 | // We'll use this number to store in the result filename... |
| 335 | var result bytes.Buffer |
| 336 | _, _ = result.WriteString(strconv.Itoa(dir) + ".") |
| 337 | |
| 338 | // but we'll augment it with the nameKey for real calculation |
| 339 | for i := range len(c.nameKey) { |
| 340 | dir += int(c.nameKey[i]) |
| 341 | } |
| 342 | |
| 343 | // Now for each character, depending on the range it is in |
| 344 | // we will actually rotate a different amount |
| 345 | for _, runeValue := range plaintext { |
| 346 | switch { |
| 347 | case runeValue == obfuscQuoteRune: |
| 348 | // Quote the Quote character |
| 349 | _, _ = result.WriteRune(obfuscQuoteRune) |
| 350 | _, _ = result.WriteRune(obfuscQuoteRune) |
| 351 | |
| 352 | case runeValue >= '0' && runeValue <= '9': |
| 353 | // Number |
| 354 | thisdir := (dir % 9) + 1 |
| 355 | newRune := '0' + (int(runeValue)-'0'+thisdir)%10 |
| 356 | _, _ = result.WriteRune(rune(newRune)) |
| 357 | |
| 358 | case (runeValue >= 'A' && runeValue <= 'Z') || |
| 359 | (runeValue >= 'a' && runeValue <= 'z'): |
| 360 | // ASCII letter. Try to avoid trivial A->a mappings |
| 361 | thisdir := dir%25 + 1 |
| 362 | // Calculate the offset of this character in A-Za-z |
| 363 | pos := int(runeValue - 'A') |
| 364 | if pos >= 26 { |
| 365 | pos -= 6 // It's lower case |
| 366 | } |
| 367 | // Rotate the character to the new location |
| 368 | pos = (pos + thisdir) % 52 |
| 369 | if pos >= 26 { |
| 370 | pos += 6 // and handle lower case offset again |
| 371 | } |
| 372 | _, _ = result.WriteRune(rune('A' + pos)) |
no test coverage detected