LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package des | 5 package des |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "testing" | 9 "testing" |
10 ) | 10 ) |
(...skipping 1486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1497 | 1497 |
1498 out := make([]byte, len(tt.out)) | 1498 out := make([]byte, len(tt.out)) |
1499 c.Decrypt(out, tt.out) | 1499 c.Decrypt(out, tt.out) |
1500 | 1500 |
1501 if !bytes.Equal(out, tt.in) { | 1501 if !bytes.Equal(out, tt.in) { |
1502 t.Errorf("#%d: result: %x want: %x", i, out, tt.in) | 1502 t.Errorf("#%d: result: %x want: %x", i, out, tt.in) |
1503 } | 1503 } |
1504 } | 1504 } |
1505 } | 1505 } |
1506 | 1506 |
| 1507 func TestInitialPermute(t *testing.T) { |
| 1508 for i := uint(0); i < 64; i++ { |
| 1509 bit := uint64(1) << i |
| 1510 got := permuteInitialBlock(bit) |
| 1511 want := uint64(1) << finalPermutation[63-i] |
| 1512 if got != want { |
| 1513 t.Errorf("permute(%x) = %x, want %x", bit, got, want) |
| 1514 } |
| 1515 } |
| 1516 } |
| 1517 |
| 1518 func TestFinalPermute(t *testing.T) { |
| 1519 for i := uint(0); i < 64; i++ { |
| 1520 bit := uint64(1) << i |
| 1521 got := permuteFinalBlock(bit) |
| 1522 want := uint64(1) << initialPermutation[63-i] |
| 1523 if got != want { |
| 1524 t.Errorf("permute(%x) = %x, want %x", bit, got, want) |
| 1525 } |
| 1526 } |
| 1527 } |
| 1528 |
| 1529 func TestExpandBlock(t *testing.T) { |
| 1530 for i := uint(0); i < 32; i++ { |
| 1531 bit := uint32(1) << i |
| 1532 got := expandBlock(bit) |
| 1533 want := permuteBlock(uint64(bit), expansionFunction[:]) |
| 1534 if got != want { |
| 1535 t.Errorf("expand(%x) = %x, want %x", bit, got, want) |
| 1536 } |
| 1537 } |
| 1538 } |
| 1539 |
1507 func BenchmarkEncrypt(b *testing.B) { | 1540 func BenchmarkEncrypt(b *testing.B) { |
1508 tt := encryptDESTests[0] | 1541 tt := encryptDESTests[0] |
1509 c, err := NewCipher(tt.key) | 1542 c, err := NewCipher(tt.key) |
1510 if err != nil { | 1543 if err != nil { |
1511 b.Fatal("NewCipher:", err) | 1544 b.Fatal("NewCipher:", err) |
1512 } | 1545 } |
1513 out := make([]byte, len(tt.in)) | 1546 out := make([]byte, len(tt.in)) |
1514 b.SetBytes(int64(len(out))) | 1547 b.SetBytes(int64(len(out))) |
1515 b.ResetTimer() | 1548 b.ResetTimer() |
1516 for i := 0; i < b.N; i++ { | 1549 for i := 0; i < b.N; i++ { |
1517 c.Encrypt(out, tt.in) | 1550 c.Encrypt(out, tt.in) |
1518 } | 1551 } |
1519 } | 1552 } |
1520 | 1553 |
1521 func BenchmarkDecrypt(b *testing.B) { | 1554 func BenchmarkDecrypt(b *testing.B) { |
1522 tt := encryptDESTests[0] | 1555 tt := encryptDESTests[0] |
1523 c, err := NewCipher(tt.key) | 1556 c, err := NewCipher(tt.key) |
1524 if err != nil { | 1557 if err != nil { |
1525 b.Fatal("NewCipher:", err) | 1558 b.Fatal("NewCipher:", err) |
1526 } | 1559 } |
1527 out := make([]byte, len(tt.out)) | 1560 out := make([]byte, len(tt.out)) |
1528 b.SetBytes(int64(len(out))) | 1561 b.SetBytes(int64(len(out))) |
1529 b.ResetTimer() | 1562 b.ResetTimer() |
1530 for i := 0; i < b.N; i++ { | 1563 for i := 0; i < b.N; i++ { |
1531 c.Decrypt(out, tt.out) | 1564 c.Decrypt(out, tt.out) |
1532 } | 1565 } |
1533 } | 1566 } |
LEFT | RIGHT |